| 1 | package DAO.impl;
|
|---|
| 2 |
|
|---|
| 3 | import DAO.JDBCUtils;
|
|---|
| 4 | import DAO.SubjectDAO;
|
|---|
| 5 | import model.Subject;
|
|---|
| 6 |
|
|---|
| 7 | import java.sql.*;
|
|---|
| 8 | import java.util.ArrayList;
|
|---|
| 9 | import java.util.List;
|
|---|
| 10 |
|
|---|
| 11 | public class SubjectDAOImpl implements SubjectDAO {
|
|---|
| 12 |
|
|---|
| 13 | @Override
|
|---|
| 14 | public List<Subject> getAll() throws SQLException {
|
|---|
| 15 | final String SELECT_ALL = "SELECT id, name, semester, credits FROM Subject";
|
|---|
| 16 | List<Subject> subjectList = new ArrayList<>();
|
|---|
| 17 |
|
|---|
| 18 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 19 | PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL);
|
|---|
| 20 | ResultSet rs = preparedStatement.executeQuery()) {
|
|---|
| 21 |
|
|---|
| 22 | while (rs.next()) {
|
|---|
| 23 | subjectList.add(mapRowToSubject(rs));
|
|---|
| 24 | }
|
|---|
| 25 | } catch (SQLException e) {
|
|---|
| 26 | System.err.println("Error fetching all Subjects: " + e.getMessage());
|
|---|
| 27 | throw e;
|
|---|
| 28 | }
|
|---|
| 29 | return subjectList;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | @Override
|
|---|
| 33 | public List<Subject> getSubjectsByProfessorId(Long professorId) throws SQLException {
|
|---|
| 34 | final String QUERY = "SELECT id, name, semester, credits FROM Subject WHERE professorid = ?";
|
|---|
| 35 | List<Subject> subjectList = new ArrayList<>();
|
|---|
| 36 |
|
|---|
| 37 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 38 | PreparedStatement ps = connection.prepareStatement(QUERY)) {
|
|---|
| 39 |
|
|---|
| 40 | ps.setLong(1, professorId);
|
|---|
| 41 | try (ResultSet rs = ps.executeQuery()) {
|
|---|
| 42 | while (rs.next()) {
|
|---|
| 43 | subjectList.add(mapRowToSubject(rs));
|
|---|
| 44 | }
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 | return subjectList;
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | @Override
|
|---|
| 51 | public Subject getByID(Long id) throws SQLException {
|
|---|
| 52 | final String QUERY = "SELECT id, name, semester, credits FROM Subject WHERE id = ?";
|
|---|
| 53 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 54 | PreparedStatement ps = connection.prepareStatement(QUERY)) {
|
|---|
| 55 |
|
|---|
| 56 | ps.setLong(1, id);
|
|---|
| 57 | try (ResultSet rs = ps.executeQuery()) {
|
|---|
| 58 | if (rs.next()) {
|
|---|
| 59 | return mapRowToSubject(rs);
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 | return null;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | @Override
|
|---|
| 67 | public void update(Subject subject) throws SQLException {
|
|---|
| 68 | final String UPDATE_SQL = "UPDATE Subject SET name = ?, semester = ?, credits = ?, facultyid = ? WHERE id = ?";
|
|---|
| 69 |
|
|---|
| 70 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 71 | PreparedStatement ps = connection.prepareStatement(UPDATE_SQL)) {
|
|---|
| 72 |
|
|---|
| 73 | ps.setString(1, subject.getName());
|
|---|
| 74 | // UPDATED: Now uses setInt because both model and DB are int
|
|---|
| 75 | ps.setInt(2, subject.getSemester());
|
|---|
| 76 | ps.setInt(3, subject.getCredits());
|
|---|
| 77 | ps.setLong(4, subject.getFacultyid());
|
|---|
| 78 | ps.setLong(5, subject.getId());
|
|---|
| 79 |
|
|---|
| 80 | ps.executeUpdate();
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | @Override
|
|---|
| 85 | public void save(Subject subject) throws SQLException {
|
|---|
| 86 | final String INSERT_SQL = "INSERT INTO Subject (name, semester, credits, facultyid) VALUES (?, ?, ?, ?)";
|
|---|
| 87 |
|
|---|
| 88 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 89 | PreparedStatement ps = connection.prepareStatement(INSERT_SQL)) {
|
|---|
| 90 |
|
|---|
| 91 | ps.setString(1, subject.getName());
|
|---|
| 92 | ps.setInt(2, subject.getSemester());
|
|---|
| 93 | ps.setInt(3, subject.getCredits());
|
|---|
| 94 | ps.setLong(4, subject.getFacultyid());
|
|---|
| 95 |
|
|---|
| 96 | ps.executeUpdate();
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | @Override
|
|---|
| 101 | public void delete(Long id) throws SQLException {
|
|---|
| 102 | final String DELETE_SQL = "DELETE FROM Subject WHERE id = ?";
|
|---|
| 103 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 104 | PreparedStatement ps = connection.prepareStatement(DELETE_SQL)) {
|
|---|
| 105 | ps.setLong(1, id);
|
|---|
| 106 | ps.executeUpdate();
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | private Subject mapRowToSubject(ResultSet rs) throws SQLException {
|
|---|
| 111 | Long id = rs.getLong("id");
|
|---|
| 112 | String name = rs.getString("name");
|
|---|
| 113 | // UPDATED: Read as int from ResultSet
|
|---|
| 114 | int semester = rs.getInt("semester");
|
|---|
| 115 | int credits = rs.getInt("credits");
|
|---|
| 116 |
|
|---|
| 117 | // Returns new Subject(Long, String, int, int, Object)
|
|---|
| 118 | return new Subject(id, name, semester, credits, null);
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | @Override
|
|---|
| 122 | public List<Subject> getPassedSubjectsByStudentId(Long studentId) throws SQLException {
|
|---|
| 123 | final String QUERY = "SELECT sub.id, sub.name, sub.semester, sub.credits " +
|
|---|
| 124 | "FROM Subject sub " +
|
|---|
| 125 | "JOIN student_subject ss ON sub.id = ss.subject_id " +
|
|---|
| 126 | "WHERE ss.student_id = ?";
|
|---|
| 127 |
|
|---|
| 128 | List<Subject> subjects = new ArrayList<>();
|
|---|
| 129 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 130 | PreparedStatement ps = connection.prepareStatement(QUERY)) {
|
|---|
| 131 |
|
|---|
| 132 | ps.setLong(1, studentId);
|
|---|
| 133 | try (ResultSet rs = ps.executeQuery()) {
|
|---|
| 134 | while (rs.next()) {
|
|---|
| 135 | subjects.add(mapRowToSubject(rs));
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | }
|
|---|
| 139 | return subjects;
|
|---|
| 140 | }
|
|---|
| 141 | } |
|---|