Guest User

Untitled

a guest
Feb 6th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. package school.database;
  2.  
  3. import school.database.Grade;
  4. import school.database.SchoolClass;
  5. import school.database.Student;
  6.  
  7. import java.sql.*;
  8. import java.util.ArrayList;
  9.  
  10.  
  11. public class DatabaseDAO {
  12.  
  13. private static final String URL = "jdbc:mysql://localhost:3306/school_db?useTimezone=true&serverTimezone=GMT";
  14. private static final String USER = "root";
  15. private static final String PASSWORD = "123456";
  16.  
  17. private static final String GET_ALL_CLASSES_QUERY =
  18. "SELECT id, name FROM class ORDER BY name";
  19.  
  20. private static final String GET_STUDENTS_IN_CLASS_QUERY =
  21. "SELECT * FROM student WHERE class_id = ?";
  22.  
  23. private static final String GET_STUDENT_GRADES_QUERY =
  24. "SELECT s.name, gv.values, g.grade_date, t.fullnamen" +
  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.grade_id = gv.idn" +
  31. "JOIN teacher tn" +
  32. "ON g.teacher_id = t.idn" +
  33. "WHERE st.id = ?";
  34.  
  35. private static final String PUT_CLASS_IN_QUERY = "INSERT INTO class (name) VALUES (?)";
  36.  
  37. private Connection conn;
  38.  
  39. public DatabaseDAO() {
  40. try {
  41. conn = DriverManager.getConnection(URL, USER, PASSWORD);
  42. } catch (SQLException e) {
  43. throw new RuntimeException(e);
  44. }
  45. }
  46.  
  47. public ArrayList<SchoolClass> readAllClassesFromDB() {
  48. ArrayList<SchoolClass> result = new ArrayList<>();
  49.  
  50. try (PreparedStatement stmt = conn.prepareStatement(GET_ALL_CLASSES_QUERY)) {
  51. ResultSet rs = stmt.executeQuery();
  52. while (rs.next()) {
  53. int id = rs.getInt("id");
  54. String name = rs.getString("name");
  55. SchoolClass schoolClass = new SchoolClass(id, name);
  56. result.add(schoolClass);
  57. }
  58. } catch (SQLException e) {
  59. throw new RuntimeException(e);
  60. }
  61.  
  62. return result;
  63. }
  64.  
  65. public ArrayList<Student> readStudentsInClassFromDB(int classID) {
  66. ArrayList<Student> result = new ArrayList<>();
  67.  
  68. try (PreparedStatement stmt = conn.prepareStatement(GET_STUDENTS_IN_CLASS_QUERY)) {
  69. stmt.setInt(1, classID);
  70. ResultSet rs = stmt.executeQuery();
  71. while (rs.next()) {
  72. int studentID = rs.getInt("id");
  73. String firstName = rs.getString("name");
  74. String lastName = rs.getString("surname");
  75. Date birthday = rs.getDate("birthday");
  76. Student student = new Student(studentID, firstName, lastName, "PATRON", birthday);
  77. result.add(student);
  78. }
  79. } catch (SQLException e) {
  80. throw new RuntimeException(e);
  81. }
  82.  
  83. return result;
  84. }
  85.  
  86. public ArrayList<Grade> readGradesFromDB(int studentID) {
  87. ArrayList<Grade> result = new ArrayList<>();
  88.  
  89. try (PreparedStatement stmt = conn.prepareStatement(GET_STUDENT_GRADES_QUERY)) {
  90. stmt.setInt(1, studentID);
  91. ResultSet rs = stmt.executeQuery();
  92. while (rs.next()) {
  93. String subject = rs.getString("name");
  94. int value = rs.getInt("values");
  95. java.util.Date date = rs.getDate("grade_date");
  96. String teacher = rs.getString("fullname");
  97. Grade grade = new Grade(subject, value, date);
  98. grade.setTeacher(teacher);
  99. result.add(grade);
  100. }
  101. } catch (SQLException e) {
  102. throw new RuntimeException(e);
  103. }
  104.  
  105. return result;
  106. }
  107.  
  108.  
  109.  
  110. public boolean writeClassinDB(Class class) {
  111. boolean result = false;
  112. PreparedStatement stmt = conn.prepareStatement(PUT_CLASS_IN_QUERY);
  113. try {
  114. stmt.setString(1, class.getName());
  115.  
  116. if(stmt.executeUpdate() == 1){
  117.  
  118. result = true;
  119. }
  120.  
  121. } catch (SQLException e) {
  122. throw new RuntimeException(e);
  123. }
  124.  
  125. return result;
  126. }
  127. }
  128.  
  129. package school.frames;
  130.  
  131. import school.Main;
  132. import school.database.SchoolClass;
  133.  
  134. import javax.management.openmbean.KeyAlreadyExistsException;
  135. import javax.swing.*;
  136.  
  137. public class CreateClassFrame extends MyFrame {
  138.  
  139. private static Integer[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  140. private static Character[] letters = {'А', 'Б', 'В', 'Г'};
  141.  
  142. public CreateClassFrame(JFrame parent) {
  143. super("Создать класс", parent, 250, 150);
  144. }
  145.  
  146. public void showFrame() {
  147. JLabel numLabel = new JLabel("Номер класса: ");
  148. numLabel.setBounds(10, 10, 150, 25);
  149. JComboBox<Integer> numsBox = new JComboBox<>(nums);
  150. numsBox.setBounds(150, 10, 90, 25);
  151.  
  152. JLabel letterLabel = new JLabel("Буква класса: ");
  153. letterLabel.setBounds(10, 45, 150, 25);
  154. JComboBox<Character> lettersBox = new JComboBox<>(letters);
  155. lettersBox.setBounds(150, 45, 90, 25);
  156.  
  157. JButton cancel = new JButton("Отмена");
  158. cancel.setBounds(10, 90, 110, 25);
  159. cancel.addActionListener(actionEvent -> dispose());
  160.  
  161. JButton done = new JButton("Готово");
  162. done.setBounds(130, 90, 110, 25);
  163. done.addActionListener(actionEvent -> {
  164. try {
  165. if (numsBox.getSelectedItem() != null && lettersBox.getSelectedItem() != null) {
  166. Main.addClass(new SchoolClass(0, "" + (Integer) numsBox.getSelectedItem() + (Character) lettersBox.getSelectedItem()));
  167. dispose();
  168. }
  169. } catch (KeyAlreadyExistsException e) {
  170. JOptionPane.showMessageDialog(this, "Класс " + e.getMessage() + " уже существует");
  171. }
  172. });
  173.  
  174. add(numLabel);
  175. add(numsBox);
  176. add(letterLabel);
  177. add(lettersBox);
  178. add(cancel);
  179. add(done);
  180.  
  181. setVisible(true);
  182. }
  183.  
  184. }
Add Comment
Please, Sign In to add comment