| 1 | package DAO.impl;
|
|---|
| 2 |
|
|---|
| 3 | import DAO.JDBCUtils;
|
|---|
| 4 | import model.ProfessorSubject;
|
|---|
| 5 | import model.Professor;
|
|---|
| 6 | import model.Subject;
|
|---|
| 7 | import java.sql.*;
|
|---|
| 8 | import java.util.ArrayList;
|
|---|
| 9 | import java.util.List;
|
|---|
| 10 |
|
|---|
| 11 | public class ProfessorSubjectDAOImpl {
|
|---|
| 12 |
|
|---|
| 13 | public void assign(Long professor_id, Long subject_id) throws SQLException {
|
|---|
| 14 | final String SQL = "INSERT INTO subject_professor (professor_id, subject_id) VALUES (?, ?)";
|
|---|
| 15 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 16 | PreparedStatement ps = conn.prepareStatement(SQL)) {
|
|---|
| 17 | ps.setLong(1, professor_id);
|
|---|
| 18 | ps.setLong(2, subject_id);
|
|---|
| 19 | ps.executeUpdate();
|
|---|
| 20 | }
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | public void remove(Long professor_id, Long subject_id) throws SQLException {
|
|---|
| 24 | final String SQL = "DELETE FROM subject_professor WHERE professor_id = ? AND subject_id = ?";
|
|---|
| 25 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 26 | PreparedStatement ps = conn.prepareStatement(SQL)) {
|
|---|
| 27 | ps.setLong(1, professor_id);
|
|---|
| 28 | ps.setLong(2, subject_id);
|
|---|
| 29 | ps.executeUpdate();
|
|---|
| 30 | }
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | public List<Subject> getSubjectsByProfessor(Long professor_id) throws SQLException {
|
|---|
| 34 | List<Subject> subjects = new ArrayList<>();
|
|---|
| 35 | final String SQL = "SELECT s.* FROM subject s " +
|
|---|
| 36 | "JOIN subject_professor ps ON s.id = ps.subject_id " +
|
|---|
| 37 | "WHERE ps.professor_id = ?";
|
|---|
| 38 |
|
|---|
| 39 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 40 | PreparedStatement ps = conn.prepareStatement(SQL)) {
|
|---|
| 41 | ps.setLong(1, professor_id);
|
|---|
| 42 | ResultSet rs = ps.executeQuery();
|
|---|
| 43 | while (rs.next()) {
|
|---|
| 44 | subjects.add(new Subject(
|
|---|
| 45 | rs.getLong("id"),
|
|---|
| 46 | rs.getString("name"),
|
|---|
| 47 | rs.getInt("semester"),
|
|---|
| 48 | rs.getInt("credits"),
|
|---|
| 49 | rs.getLong("facultyid")
|
|---|
| 50 | ));
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 | return subjects;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | public List<Professor> getProfessorsBySubject(Long subject_id) throws SQLException {
|
|---|
| 57 | List<Professor> professors = new ArrayList<>();
|
|---|
| 58 | final String SQL = "SELECT p.* FROM professor p " +
|
|---|
| 59 | "JOIN subject_professor ps ON p.id = ps.professor_id " +
|
|---|
| 60 | "WHERE ps.subject_id = ?";
|
|---|
| 61 |
|
|---|
| 62 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 63 | PreparedStatement ps = conn.prepareStatement(SQL)) {
|
|---|
| 64 | ps.setLong(1, subject_id);
|
|---|
| 65 | ResultSet rs = ps.executeQuery();
|
|---|
| 66 | while (rs.next()) {
|
|---|
| 67 | professors.add(new Professor(
|
|---|
| 68 | rs.getLong("id"),
|
|---|
| 69 | rs.getString("name"),
|
|---|
| 70 | rs.getString("surname"),
|
|---|
| 71 | rs.getInt("age"),
|
|---|
| 72 | rs.getLong("facultyid"),
|
|---|
| 73 | null
|
|---|
| 74 | ));
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|
| 77 | return professors;
|
|---|
| 78 | }
|
|---|
| 79 | } |
|---|