| 1 | package DAO.impl;
|
|---|
| 2 |
|
|---|
| 3 | import DAO.JDBCUtils;
|
|---|
| 4 | import model.StudentSubject;
|
|---|
| 5 | import java.sql.*;
|
|---|
| 6 | import java.util.ArrayList;
|
|---|
| 7 | import java.util.List;
|
|---|
| 8 |
|
|---|
| 9 | public class StudentSubjectDAOImpl {
|
|---|
| 10 |
|
|---|
| 11 | public void save(StudentSubject ss) throws SQLException {
|
|---|
| 12 | final String SQL = "INSERT INTO student_subject (student_id, subject_id, enrollment_date, status, final_grade, absences_count) VALUES (?, ?, ?, ?, ?, ?)";
|
|---|
| 13 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 14 | PreparedStatement ps = conn.prepareStatement(SQL)) {
|
|---|
| 15 | ps.setLong(1, ss.getStudent_id());
|
|---|
| 16 | ps.setLong(2, ss.getSubject_id());
|
|---|
| 17 | ps.setDate(3, Date.valueOf(ss.getEnrollment_date()));
|
|---|
| 18 | ps.setString(4, ss.getStatus());
|
|---|
| 19 | ps.setObject(5, ss.getFinal_grade()); // Handles null grade automatically
|
|---|
| 20 | ps.setInt(6, ss.getAbsences_count());
|
|---|
| 21 | ps.executeUpdate();
|
|---|
| 22 | }
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | public List<StudentSubject> getAll() throws SQLException {
|
|---|
| 26 | List<StudentSubject> list = new ArrayList<>();
|
|---|
| 27 | final String SQL = "SELECT * FROM student_subject";
|
|---|
| 28 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 29 | Statement stmt = conn.createStatement();
|
|---|
| 30 | ResultSet rs = stmt.executeQuery(SQL)) {
|
|---|
| 31 | while (rs.next()) {
|
|---|
| 32 | list.add(mapRow(rs));
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 | return list;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | public StudentSubject getById(Long ss_id) throws SQLException {
|
|---|
| 39 | final String SQL = "SELECT * FROM student_subject WHERE ss_id = ?";
|
|---|
| 40 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 41 | PreparedStatement ps = conn.prepareStatement(SQL)) {
|
|---|
| 42 | ps.setLong(1, ss_id);
|
|---|
| 43 | ResultSet rs = ps.executeQuery();
|
|---|
| 44 | if (rs.next()) return mapRow(rs);
|
|---|
| 45 | }
|
|---|
| 46 | return null;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | public void update(StudentSubject ss) throws SQLException {
|
|---|
| 50 | final String SQL = "UPDATE student_subject SET status = ?, final_grade = ?, absences_count = ? WHERE ss_id = ?";
|
|---|
| 51 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 52 | PreparedStatement ps = conn.prepareStatement(SQL)) {
|
|---|
| 53 | ps.setString(1, ss.getStatus());
|
|---|
| 54 | ps.setObject(2, ss.getFinal_grade());
|
|---|
| 55 | ps.setInt(3, ss.getAbsences_count());
|
|---|
| 56 | ps.setLong(4, ss.getSs_id());
|
|---|
| 57 | ps.executeUpdate();
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | public void delete(Long ss_id) throws SQLException {
|
|---|
| 62 | final String SQL = "DELETE FROM student_subject WHERE ss_id = ?";
|
|---|
| 63 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 64 | PreparedStatement ps = conn.prepareStatement(SQL)) {
|
|---|
| 65 | ps.setLong(1, ss_id);
|
|---|
| 66 | ps.executeUpdate();
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | private StudentSubject mapRow(ResultSet rs) throws SQLException {
|
|---|
| 71 | return new StudentSubject(
|
|---|
| 72 | rs.getLong("ss_id"),
|
|---|
| 73 | rs.getLong("student_id"),
|
|---|
| 74 | rs.getLong("subject_id"),
|
|---|
| 75 | rs.getDate("enrollment_date").toLocalDate(),
|
|---|
| 76 | rs.getString("status"),
|
|---|
| 77 | (Integer) rs.getObject("final_grade"),
|
|---|
| 78 | rs.getInt("absences_count")
|
|---|
| 79 | );
|
|---|
| 80 | }
|
|---|
| 81 | } |
|---|