Guest User

Untitled

a guest
Feb 4th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. package school.database;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5.  
  6.  
  7. public class DatabaseDAO {
  8.  
  9. private static final String URL = "jdbc:mysql://localhost:3306/school_db?useTimezone=true&serverTimezone=GMT";
  10. private static final String USER = "root";
  11. private static final String PASSWORD = "123456";
  12.  
  13. private static final String GET_ALL_CLASSES_QUERY = "SELECT id, name FROM class ORDER BY name";
  14.  
  15. private static final String GET_STUDENTS_IN_CLASS_QUERY =
  16. "SELECT s.id, s.name, s.surname, s.birthday, s.phonen" +
  17. "FROM student s JOIN class_student_connection cscn" +
  18. "ON csc.student_id = s.idn" +
  19. "JOIN class cn" +
  20. "ON csc.class_id = c.idn" +
  21. "WHERE c.id = ?";
  22.  
  23. private static final String GET_STUDENT_GRADES_QUERY =
  24. "SELECT s.name, gv.values, g.grade_daten" +
  25. "FROM grade g JOIN subject sn" +
  26. "ON g.subject_id = s.idn" +
  27. "JOIN student stn" +
  28. "ON g.student_id = st.idn" +
  29. "JOIN grade_value gvn" +
  30. "ON g.mark_id = gv.idn" +
  31. "WHERE st.id = ?";
  32.  
  33.  
  34. private Connection conn;
  35.  
  36. public DatabaseDAO() {
  37. try {
  38. conn = DriverManager.getConnection(URL, USER, PASSWORD);
  39. } catch (SQLException e) {
  40. throw new RuntimeException(e);
  41. }
  42. }
  43.  
  44. public ArrayList<SchoolClass> readAllClassesFromDB() {
  45. ArrayList<SchoolClass> result = new ArrayList<>();
  46.  
  47. try (PreparedStatement stmt = conn.prepareStatement(GET_ALL_CLASSES_QUERY)) {
  48. ResultSet rs = stmt.executeQuery();
  49. while (rs.next()) {
  50. int id = rs.getInt("id");
  51. String name = rs.getString("name");
  52. SchoolClass schoolClass = new SchoolClass(id, name);
  53. result.add(schoolClass);
  54. }
  55. } catch (SQLException e) {
  56. throw new RuntimeException(e);
  57. }
  58.  
  59. return result;
  60. }
  61.  
  62. public ArrayList<Student> readStudentsInClassFromDB(int classID) {
  63. ArrayList<Student> result = new ArrayList<>();
  64.  
  65. try (PreparedStatement stmt = conn.prepareStatement(GET_STUDENTS_IN_CLASS_QUERY)) {
  66. stmt.setInt(1, classID);
  67. ResultSet rs = stmt.executeQuery();
  68. while (rs.next()) {
  69. int studentID = rs.getInt("id");
  70. String firstName = rs.getString("name");
  71. String lastName = rs.getString("surname");
  72. Date birthday = rs.getDate("birthday");
  73. Student student = new Student(studentID, firstName, lastName, "PATRON", birthday);
  74. result.add(student);
  75. }
  76. } catch (SQLException e) {
  77. throw new RuntimeException(e);
  78. }
  79.  
  80. return result;
  81. }
  82.  
  83. public ArrayList<Grade> readGradesFromDB(int studentID) {
  84. ArrayList<Grade> result = new ArrayList<>();
  85.  
  86. try (PreparedStatement stmt = conn.prepareStatement(GET_STUDENT_GRADES_QUERY)) {
  87. stmt.setInt(1, studentID);
  88. ResultSet rs = stmt.executeQuery();
  89. while (rs.next()) {
  90. String subject = rs.getString("name");
  91. int value = rs.getInt("values");
  92. java.util.Date date = rs.getDate("grade_date");
  93. Grade grade = new Grade(subject, value, date.toString());
  94. result.add(grade);
  95. }
  96. } catch (SQLException e) {
  97. throw new RuntimeException(e);
  98. }
  99.  
  100. return result;
  101. }
  102. }
Add Comment
Please, Sign In to add comment