| [1eb7a55] | 1 | package DAO.impl;
|
|---|
| 2 |
|
|---|
| 3 | import DAO.JDBCUtils;
|
|---|
| 4 | import DAO.AdviceDAO;
|
|---|
| 5 | import model.Advice;
|
|---|
| 6 | import java.sql.*;
|
|---|
| 7 | import java.util.ArrayList;
|
|---|
| 8 | import java.util.List;
|
|---|
| 9 |
|
|---|
| 10 | public class AdviceDAOImpl implements AdviceDAO {
|
|---|
| 11 |
|
|---|
| 12 | @Override
|
|---|
| 13 | public void save(Advice advice) throws SQLException {
|
|---|
| 14 | final String INSERT = "INSERT INTO advice (student_id, professor_id, start_date, end_date) VALUES (?, ?, ?, ?)";
|
|---|
| 15 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 16 | PreparedStatement ps = conn.prepareStatement(INSERT)) {
|
|---|
| 17 | ps.setInt(1, advice.getstudent_id());
|
|---|
| 18 | ps.setInt(2, advice.getprofessor_id());
|
|---|
| 19 | ps.setDate(3, advice.getstart_date());
|
|---|
| 20 | ps.setDate(4, advice.getend_date());
|
|---|
| 21 | ps.executeUpdate();
|
|---|
| 22 | }
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | @Override
|
|---|
| 26 | public void update(Advice advice) throws SQLException {
|
|---|
| 27 | final String UPDATE = "UPDATE advice SET start_date = ?, end_date = ? WHERE student_id = ? AND professor_id = ?";
|
|---|
| 28 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 29 | PreparedStatement ps = conn.prepareStatement(UPDATE)) {
|
|---|
| 30 | ps.setDate(1, advice.getstart_date());
|
|---|
| 31 | ps.setDate(2, advice.getend_date());
|
|---|
| 32 | ps.setInt(3, advice.getstudent_id());
|
|---|
| 33 | ps.setInt(4, advice.getprofessor_id());
|
|---|
| 34 | ps.executeUpdate();
|
|---|
| 35 | }
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | @Override
|
|---|
| 39 | public void delete(int studentId, int professorId) throws SQLException {
|
|---|
| 40 | final String DELETE = "DELETE FROM advice WHERE student_id = ? AND professor_id = ?";
|
|---|
| 41 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 42 | PreparedStatement ps = conn.prepareStatement(DELETE)) {
|
|---|
| 43 | ps.setInt(1, studentId);
|
|---|
| 44 | ps.setInt(2, professorId);
|
|---|
| 45 | ps.executeUpdate();
|
|---|
| 46 | }
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | @Override
|
|---|
| 50 | public Advice getById(int studentId, int professorId) throws SQLException {
|
|---|
| 51 | final String SELECT_BY_ID = "SELECT * FROM advice WHERE student_id = ? AND professor_id = ?";
|
|---|
| 52 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 53 | PreparedStatement ps = conn.prepareStatement(SELECT_BY_ID)) {
|
|---|
| 54 | ps.setInt(1, studentId);
|
|---|
| 55 | ps.setInt(2, professorId);
|
|---|
| 56 | try (ResultSet rs = ps.executeQuery()) {
|
|---|
| 57 | if (rs.next()) {
|
|---|
| 58 | return new Advice(
|
|---|
| 59 | rs.getInt("student_id"),
|
|---|
| 60 | rs.getInt("professor_id"),
|
|---|
| 61 | rs.getDate("start_date"),
|
|---|
| 62 | rs.getDate("end_date")
|
|---|
| 63 | );
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 | return null;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | @Override
|
|---|
| 71 | public List<Advice> getAll() throws SQLException {
|
|---|
| 72 | List<Advice> list = new ArrayList<>();
|
|---|
| 73 | final String SELECT_ALL = "SELECT * FROM advice";
|
|---|
| 74 | try (Connection conn = JDBCUtils.getConnection();
|
|---|
| 75 | Statement st = conn.createStatement();
|
|---|
| 76 | ResultSet rs = st.executeQuery(SELECT_ALL)) {
|
|---|
| 77 | while (rs.next()) {
|
|---|
| 78 | list.add(new Advice(
|
|---|
| 79 | rs.getInt("student_id"),
|
|---|
| 80 | rs.getInt("professor_id"),
|
|---|
| 81 | rs.getDate("start_date"),
|
|---|
| 82 | rs.getDate("end_date")
|
|---|
| 83 | ));
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 | return list;
|
|---|
| 87 | }
|
|---|
| 88 | } |
|---|