Advertisement
Guest User

data

a guest
Sep 25th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.03 KB | None | 0 0
  1. package DataAccessLayer;
  2.  
  3.  
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.PreparedStatement;
  8. import java.sql.ResultSet;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. import Model.*;
  15.  
  16.  
  17. public class Dal {
  18.  
  19. public static Connection getConnection() throws SQLException{
  20. String sqlConnect = "jdbc:sqlserver://localhost;databasename=SQLDATABAS;user=sa;password=123";
  21. return DriverManager.getConnection(sqlConnect);
  22. }
  23.  
  24. public static void test(){
  25. Connection con = null;
  26. try {
  27. con = getConnection();
  28. } catch (SQLException e) {
  29.  
  30. }
  31.  
  32. if(con == null){
  33. System.out.println("funkar inte");
  34. }else{
  35. System.out.println("funkar");
  36. }
  37. }
  38.  
  39.  
  40. public static void createStudent(String spnr, String sname) throws Exception {
  41. PreparedStatement pstm;
  42. String sqlString;
  43. Connection con = getConnection();
  44.  
  45. sqlString = "INSERT INTO student (spnr, sname) VALUES(?,?)";
  46.  
  47.  
  48. try{
  49.  
  50. pstm = con.prepareStatement(sqlString);
  51. pstm.setString(1, spnr);
  52. pstm.setString(2, sname);
  53. pstm.executeUpdate();
  54.  
  55. } catch(SQLException e){
  56.  
  57. }
  58. }
  59.  
  60. public static void createCourse(String ccode, String cname, String cpoints) throws Exception {
  61. PreparedStatement pstm;
  62. String sqlString;
  63. Connection con = getConnection();
  64. sqlString = "INSERT INTO Course (ccode, cname, cpoints) VALUES (?,?,?)";
  65.  
  66. try {
  67. pstm = con.prepareStatement(sqlString);
  68. pstm.setString(1, ccode);
  69. pstm.setString(2, cname);
  70. pstm.setString(3, cpoints);
  71. pstm.executeUpdate();
  72. }catch (SQLException e){
  73.  
  74. }
  75. }
  76.  
  77.  
  78.  
  79.  
  80. public static Course findCourse (String courseCode) throws Exception{
  81. PreparedStatement pstm;
  82. ResultSet rSet;
  83. Connection con = Dal.getConnection();
  84. String sqlString;
  85. Course c = null;
  86.  
  87. sqlString = "SELECT * FROM Course WHERE ccode = ?";
  88.  
  89. try {
  90. pstm = con.prepareStatement(sqlString);
  91. pstm.setString(1, courseCode );
  92. rSet = pstm.executeQuery();
  93. if (rSet.next()){
  94. c = new Course();
  95. c.setCcode(courseCode);
  96. c.setCname(rSet.getString("Coursename"));
  97. c.setCpoints(rSet.getString(0));
  98. }
  99. }catch (Exception e){
  100. }
  101. return c;
  102. }
  103.  
  104.  
  105. public static void createStudentOnReading (String spnr, String ccode, String term)throws Exception {
  106. PreparedStatement pstm;
  107. Connection con = Dal.getConnection();
  108.  
  109. String sqlString ="INSERT INTO reading (spnr, ccode, termin) VALUES (?,?,?)";
  110.  
  111. try {
  112. pstm = con.prepareStatement(sqlString);
  113. pstm.setString(1, spnr);
  114. pstm.setString(2, ccode);
  115. pstm.setString(3, term);
  116. pstm.executeUpdate();
  117.  
  118. }catch (SQLException e){
  119. }
  120. }
  121.  
  122.  
  123.  
  124. public static void createStudentOnDonereading (String ccode, String spnr, String grade, String term)throws Exception {
  125. PreparedStatement pstm;
  126. Connection con = Dal.getConnection();
  127. String sqlString;
  128. Donereading dr = new Donereading();
  129. dr.getSpnr();
  130. dr.getCcode();
  131. dr.getGrade();
  132. dr.getTerm();
  133.  
  134. sqlString = "INSERT INTO donereading (spnr, ccode, betyg, termin) VALUES(?,?,?,?)";
  135.  
  136. try{
  137. pstm = con.prepareStatement(sqlString);
  138. pstm.setString(1, spnr);
  139. pstm.setString(2, ccode);
  140. pstm.setString(3, grade);
  141. pstm.setString(4, term);
  142. pstm.executeUpdate();
  143. }catch (SQLException e){
  144. }
  145. }
  146.  
  147.  
  148.  
  149.  
  150. public static List<Donereading> searchGrade (String spnr) throws Exception {
  151. List<Donereading> list = new ArrayList<>();
  152. PreparedStatement pstm;
  153. ResultSet rSet;
  154. Connection con = Dal.getConnection();
  155. try{
  156. spnr+="%";
  157. pstm = con.prepareStatement("select * from donereading where spnr like ?");
  158. pstm.setString(1, spnr);
  159. rSet = pstm.executeQuery();
  160.  
  161. while(rSet.next()){
  162. Donereading tempDone = convertRowToGrade(rSet);
  163. list.add(tempDone);
  164. }
  165.  
  166. } catch (Exception e){
  167. }
  168. return list;
  169. }
  170.  
  171.  
  172. private static Donereading convertRowToGrade(ResultSet rSet) throws Exception {
  173.  
  174. String ccode = rSet.getString("ccode");
  175. String term =rSet.getString("termin");
  176. String grade = rSet.getString("betyg");
  177. String spnr =rSet.getString("spnr");
  178.  
  179. Donereading tempDone = new Donereading(ccode,spnr,grade,term);
  180. return tempDone;
  181. }
  182.  
  183.  
  184.  
  185.  
  186. public static ArrayList<Donereading> findDonereading (String ccode) throws Exception {
  187. PreparedStatement pstm;
  188. Connection con = Dal.getConnection();
  189. String sqlString;
  190. ResultSet rSet;
  191.  
  192. sqlString ="select * from donereading where ccode = ?";
  193.  
  194. ArrayList<Donereading> lista = new ArrayList<Donereading>();
  195.  
  196. try {
  197. pstm = con.prepareStatement(sqlString);
  198. pstm.setString(1, ccode);
  199.  
  200. rSet = pstm.executeQuery();
  201. while(rSet.next()){
  202. Donereading dr = new Donereading();
  203. dr.setSpnr(rSet.getString("spnr"));
  204. dr.setTerm(rSet.getString("termin"));
  205. dr.setGrade(rSet.getString("betyg"));
  206. dr.setCcode(rSet.getString("ccode"));
  207. lista.add(dr);
  208. }
  209.  
  210.  
  211. }catch (SQLException e){
  212. }
  213. return lista;
  214. }
  215.  
  216.  
  217.  
  218.  
  219.  
  220. public static List<Student> searchStudents (String spnr) throws Exception {
  221. List<Student> list = new ArrayList<>();
  222. PreparedStatement pstm;
  223. ResultSet rSet;
  224. Connection con = Dal.getConnection();
  225. try{
  226. spnr+="%";
  227. pstm = con.prepareStatement("select * from student where spnr like ?");
  228. pstm.setString(1, spnr);
  229. rSet = pstm.executeQuery();
  230.  
  231. while(rSet.next()){
  232. Student tempStudent = convertRowToStudents(rSet);
  233. list.add(tempStudent);
  234. }
  235.  
  236. } catch (Exception e){
  237. }
  238. return list;
  239. }
  240.  
  241. public static List<Student> getAllStudents () throws Exception {
  242. List<Student> list = new ArrayList<Student>();
  243.  
  244. Connection con = Dal.getConnection();
  245. Statement stm;
  246. ResultSet rSet;
  247. try{
  248. stm = con.createStatement();
  249. rSet = stm.executeQuery("select * from student");
  250.  
  251. while (rSet.next()){
  252. Student tempStudents = convertRowToStudents(rSet);
  253. list.add(tempStudents);
  254.  
  255. }
  256.  
  257. }catch (Exception ex){
  258. }
  259. return list;
  260.  
  261. }
  262.  
  263. private static Student convertRowToStudents(ResultSet rSet) throws Exception {
  264.  
  265. String spnr = rSet.getString("spnr");
  266. String name = rSet.getString("sname");
  267.  
  268. Student tempStudents = new Student(spnr,name);
  269. return tempStudents;
  270. }
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278. public static List <Course> searchCourses (String ccode) throws Exception {
  279. List<Course> list = new ArrayList<>();
  280. PreparedStatement pstm;
  281. ResultSet rSet;
  282. Connection con = Dal.getConnection();
  283. try{
  284. ccode+="%";
  285. pstm = con.prepareStatement("select * from course where ccode like ?");
  286. pstm.setString(1, ccode);
  287. rSet = pstm.executeQuery();
  288.  
  289. while(rSet.next()){
  290. Course tempCourses = convertRowToCourse(rSet);
  291. list.add(tempCourses);
  292. }
  293.  
  294. } catch (Exception e){
  295. }
  296. return list;
  297. }
  298.  
  299. public static List<Course> getAllCourses () throws Exception {
  300. List<Course> courselist = new ArrayList<Course>();
  301.  
  302. Connection con = Dal.getConnection();
  303. Statement stm;
  304. ResultSet rSet;
  305. try{
  306. stm = con.createStatement();
  307. rSet = stm.executeQuery("select * from course");
  308.  
  309. while (rSet.next()){
  310. Course tempCourses = convertRowToCourse(rSet);
  311. courselist.add(tempCourses);
  312. }
  313.  
  314. }catch (Exception ex){
  315.  
  316. }
  317. return courselist;
  318. }
  319.  
  320. private static Course convertRowToCourse(ResultSet rSet) throws Exception{
  321.  
  322. String ccode = rSet.getString("ccode");
  323. String cname = rSet.getString("cname");
  324. String cpoints =rSet.getString("cpoints");
  325.  
  326. Course tempCourse = new Course(ccode,cname,cpoints);
  327. return tempCourse;
  328. }
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335. public static List <Donereading> searchDonereading (String ccode) throws Exception {
  336. List<Donereading> list = new ArrayList<>();
  337. PreparedStatement pstm;
  338. ResultSet rSet;
  339. Connection con = Dal.getConnection();
  340. try{
  341. ccode +="%";
  342. pstm = con.prepareStatement("select * from donereading where ccode like ?");
  343. pstm.setString(1, ccode);
  344. rSet = pstm.executeQuery();
  345.  
  346. while(rSet.next()){
  347. Donereading tempDone = convertRowToDonereading(rSet);
  348. list.add(tempDone);
  349. }
  350.  
  351. } catch (Exception e){
  352. }
  353. return list;
  354. }
  355.  
  356. public static List<Donereading> getAllDonereading () throws Exception {
  357. List<Donereading> doneList = new ArrayList<Donereading>();
  358.  
  359. Connection con = Dal.getConnection();
  360. Statement stm;
  361. ResultSet rSet;
  362. try{
  363. stm = con.createStatement();
  364. rSet = stm.executeQuery("select * from donereading");
  365.  
  366. while (rSet.next()){
  367. Donereading tempDone = convertRowToDonereading(rSet);
  368. doneList.add(tempDone);
  369. }
  370.  
  371. }catch (Exception ex){
  372. }
  373. return doneList;
  374. }
  375.  
  376. private static Donereading convertRowToDonereading(ResultSet rSet) throws Exception {
  377.  
  378. String ccode = rSet.getString("ccode");
  379. String term =rSet.getString("termin");
  380. String grade = rSet.getString("betyg");
  381. String spnr =rSet.getString("spnr");
  382.  
  383. Donereading tempDone = new Donereading (ccode,spnr,grade,term);
  384. return tempDone;
  385. }
  386.  
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement