Как с интерфейса (JAVA)добавлять в базу данных информацию
#39770495
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
Я написала программу школьный журнал. И мне нужно через интерфейс туда добавить класс(Пример "3Б").
Работа с базой данных в JAVA
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. 100. 101. 102. 103. 104. 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. 128.
package school.database;
import school.database.Grade;
import school.database.SchoolClass;
import school.database.Student;
import java.sql.*;
import java.util.ArrayList;
public class DatabaseDAO {
private static final String URL = "jdbc:mysql://localhost:3306/school_db?useTimezone=true&serverTimezone=GMT";
private static final String USER = "root";
private static final String PASSWORD = "123456";
private static final String GET_ALL_CLASSES_QUERY =
"SELECT id, name FROM class ORDER BY name";
private static final String GET_STUDENTS_IN_CLASS_QUERY =
"SELECT * FROM student WHERE class_id = ?";
private static final String GET_STUDENT_GRADES_QUERY =
"SELECT s.name, gv.values, g.grade_date, t.fullname\n" +
"FROM grade g JOIN subject s\n" +
"ON g.subject_id = s.id\n" +
"JOIN student st\n" +
"ON g.student_id = st.id\n" +
"JOIN grade_value gv\n" +
"ON g.grade_id = gv.id\n" +
"JOIN teacher t\n" +
"ON g.teacher_id = t.id\n" +
"WHERE st.id = ?";
private static final String PUT_CLASS_IN_QUERY = "INSERT INTO class (name) VALUES (?)";
private Connection conn;
public DatabaseDAO() {
try {
conn = DriverManager.getConnection(URL, USER, PASSWORD);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public ArrayList<SchoolClass> readAllClassesFromDB() {
ArrayList<SchoolClass> result = new ArrayList<>();
try (PreparedStatement stmt = conn.prepareStatement(GET_ALL_CLASSES_QUERY)) {
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
SchoolClass schoolClass = new SchoolClass(id, name);
result.add(schoolClass);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return result;
}
public ArrayList<Student> readStudentsInClassFromDB(int classID) {
ArrayList<Student> result = new ArrayList<>();
try (PreparedStatement stmt = conn.prepareStatement(GET_STUDENTS_IN_CLASS_QUERY)) {
stmt.setInt(1, classID);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
int studentID = rs.getInt("id");
String firstName = rs.getString("name");
String lastName = rs.getString("surname");
Date birthday = rs.getDate("birthday");
Student student = new Student(studentID, firstName, lastName, "PATRON", birthday);
result.add(student);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return result;
}
public ArrayList<Grade> readGradesFromDB(int studentID) {
ArrayList<Grade> result = new ArrayList<>();
try (PreparedStatement stmt = conn.prepareStatement(GET_STUDENT_GRADES_QUERY)) {
stmt.setInt(1, studentID);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
String subject = rs.getString("name");
int value = rs.getInt("values");
java.util.Date date = rs.getDate("grade_date");
String teacher = rs.getString("fullname");
Grade grade = new Grade(subject, value, date);
grade.setTeacher(teacher);
result.add(grade);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return result;
}
public boolean writeClassinDB(Class clazz) {
boolean result = false;
try {
PreparedStatement stmt = conn.prepareStatement(PUT_CLASS_IN_QUERY);
stmt.setString(1, clazz.getName());
if(stmt.executeUpdate() == 1){
result = true;
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return result;
}
}
Вот мой интерфейс
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56.
package school.frames;
import school.Main;
import school.database.SchoolClass;
import javax.management.openmbean.KeyAlreadyExistsException;
import javax.swing.*;
public class CreateClassFrame extends MyFrame {
private static Integer[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
private static Character[] letters = {'А', 'Б', 'В', 'Г'};
public CreateClassFrame(JFrame parent) {
super("Создать класс", parent, 250, 150);
}
public void showFrame() {
JLabel numLabel = new JLabel("Номер класса: ");
numLabel.setBounds(10, 10, 150, 25);
JComboBox<Integer> numsBox = new JComboBox<>(nums);
numsBox.setBounds(150, 10, 90, 25);
JLabel letterLabel = new JLabel("Буква класса: ");
letterLabel.setBounds(10, 45, 150, 25);
JComboBox<Character> lettersBox = new JComboBox<>(letters);
lettersBox.setBounds(150, 45, 90, 25);
JButton cancel = new JButton("Отмена");
cancel.setBounds(10, 90, 110, 25);
cancel.addActionListener(actionEvent -> dispose());
JButton done = new JButton("Готово");
done.setBounds(130, 90, 110, 25);
done.addActionListener(actionEvent -> {
try {
if (numsBox.getSelectedItem() != null && lettersBox.getSelectedItem() != null) {
Main.addClass(new SchoolClass(0, "" + (Integer) numsBox.getSelectedItem() + (Character) lettersBox.getSelectedItem()));
dispose();
}
} catch (KeyAlreadyExistsException e) {
JOptionPane.showMessageDialog(this, "Класс " + e.getMessage() + " уже существует");
}
});
add(numLabel);
add(numsBox);
add(letterLabel);
add(lettersBox);
add(cancel);
add(done);
setVisible(true);
}
}
|
|