| 1 | package DAO.impl;
|
|---|
| 2 |
|
|---|
| 3 | import DAO.FacultyDAO;
|
|---|
| 4 | import DAO.JDBCUtils;
|
|---|
| 5 | import model.Faculty;
|
|---|
| 6 |
|
|---|
| 7 | import java.sql.Connection;
|
|---|
| 8 | import java.sql.PreparedStatement;
|
|---|
| 9 | import java.sql.ResultSet;
|
|---|
| 10 | import java.sql.SQLException;
|
|---|
| 11 | import java.util.ArrayList;
|
|---|
| 12 | import java.util.List;
|
|---|
| 13 | import java.util.logging.Logger;
|
|---|
| 14 |
|
|---|
| 15 | public class FacultyDAOImpl implements FacultyDAO {
|
|---|
| 16 |
|
|---|
| 17 | @Override
|
|---|
| 18 | public List<Faculty> getAll() throws SQLException {
|
|---|
| 19 |
|
|---|
| 20 | final String SELECT_ALL_Faculty = "SELECT * FROM Faculty;";
|
|---|
| 21 |
|
|---|
| 22 | List<Faculty> facultyList = new ArrayList<>();
|
|---|
| 23 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 24 | PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_Faculty)) {
|
|---|
| 25 | ResultSet rs = preparedStatement.executeQuery();
|
|---|
| 26 |
|
|---|
| 27 | while (rs.next()) {
|
|---|
| 28 | Long id = rs.getLong("id");
|
|---|
| 29 | String name = rs.getString("name");
|
|---|
| 30 | String location = rs.getString("location");
|
|---|
| 31 | String study_field= rs.getString("study_field");
|
|---|
| 32 | Long university_id = rs.getLong("university_id");
|
|---|
| 33 |
|
|---|
| 34 | Faculty faculty = new Faculty(id, name, location, study_field,university_id);
|
|---|
| 35 | facultyList.add(faculty);
|
|---|
| 36 | System.out.println(id + ", " + name + ", " + location + ", " + study_field);
|
|---|
| 37 | }
|
|---|
| 38 | } catch (SQLException e) {
|
|---|
| 39 | System.err.println("Error updating Faculty: " + e.getMessage());
|
|---|
| 40 | throw e;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | return facultyList;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | @Override
|
|---|
| 47 | public Faculty getByID(Long id) throws SQLException {
|
|---|
| 48 | final String SELECT_BY_ID = "SELECT * FROM Faculty WHERE id = ?;";
|
|---|
| 49 | Faculty faculty = null;
|
|---|
| 50 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 51 | PreparedStatement preparedStatement = connection.prepareStatement(SELECT_BY_ID)) {
|
|---|
| 52 | preparedStatement.setLong(1, id);
|
|---|
| 53 | ResultSet rs = preparedStatement.executeQuery();
|
|---|
| 54 | if(rs.next()) {
|
|---|
| 55 | String name = rs.getString("name");
|
|---|
| 56 | String location = rs.getString("location");
|
|---|
| 57 | String study_field= rs.getString("study_field");
|
|---|
| 58 | Long university_id = rs.getLong("university_id");
|
|---|
| 59 |
|
|---|
| 60 | System.out.println(id + ", " + name + ", " + location + ", " + study_field);
|
|---|
| 61 | faculty = new Faculty(id, name, location, study_field, university_id);
|
|---|
| 62 | }
|
|---|
| 63 | else {
|
|---|
| 64 | System.out.println("No Faculty found with ID " + id);
|
|---|
| 65 | }
|
|---|
| 66 | }
|
|---|
| 67 | catch (SQLException e) {
|
|---|
| 68 | System.err.println("Error updating Faculty: " + e.getMessage());
|
|---|
| 69 | throw e;
|
|---|
| 70 | //JDBCUtils.printSQLException(e);
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | return faculty;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | @Override
|
|---|
| 77 | public void update(Faculty faculty) throws SQLException {
|
|---|
| 78 |
|
|---|
| 79 | final String UPDATE_Faculty_SQL = "UPDATE Faculty SET name = ?, location = ?, study_field = ?::study_field_enum, university_id = ? WHERE id = ?";
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 83 | PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_Faculty_SQL)) {
|
|---|
| 84 | preparedStatement.setString(1, faculty.getName());
|
|---|
| 85 | preparedStatement.setString(2, faculty.getLocation());
|
|---|
| 86 | preparedStatement.setString(3, faculty.getStudy_field());
|
|---|
| 87 | preparedStatement.setLong(4, faculty.getUniversityId());
|
|---|
| 88 | preparedStatement.setLong(5, faculty.getId());
|
|---|
| 89 | preparedStatement.executeUpdate();
|
|---|
| 90 | } catch (SQLException e) {
|
|---|
| 91 | System.err.println("Error updating Faculty: " + e.getMessage());
|
|---|
| 92 | throw e;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | @Override
|
|---|
| 97 | public void save(Faculty faculty) throws SQLException {
|
|---|
| 98 | final String INSERT_Faculty_SQL = "INSERT INTO faculty (name, location, study_field, university_id) VALUES (?, ?, ?::study_field_enum, ?)";
|
|---|
| 99 |
|
|---|
| 100 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 101 | PreparedStatement preparedStatement = connection.prepareStatement(INSERT_Faculty_SQL)) {
|
|---|
| 102 | preparedStatement.setString(1, faculty.getName());
|
|---|
| 103 | preparedStatement.setString(2, faculty.getLocation());
|
|---|
| 104 | preparedStatement.setString(3, faculty.getStudy_field());
|
|---|
| 105 | preparedStatement.setLong(4, faculty.getUniversityId());
|
|---|
| 106 | preparedStatement.executeUpdate();
|
|---|
| 107 | } catch (SQLException e) {
|
|---|
| 108 | System.err.println("Error inserting Faculty: " + e.getMessage());
|
|---|
| 109 | throw e;
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 | @Override
|
|---|
| 116 | public void delete(Long id) throws SQLException {
|
|---|
| 117 | final String DELETE_PROFS = "DELETE FROM professor WHERE facultyid = ?;";
|
|---|
| 118 | final String DELETE_FACULTY = "DELETE FROM Faculty WHERE id = ?;";
|
|---|
| 119 |
|
|---|
| 120 | try (Connection connection = JDBCUtils.getConnection()) {
|
|---|
| 121 | connection.setAutoCommit(false);
|
|---|
| 122 |
|
|---|
| 123 | try (PreparedStatement st1 = connection.prepareStatement(DELETE_PROFS);
|
|---|
| 124 | PreparedStatement st2 = connection.prepareStatement(DELETE_FACULTY)) {
|
|---|
| 125 |
|
|---|
| 126 | st1.setLong(1, id);
|
|---|
| 127 | st1.executeUpdate();
|
|---|
| 128 |
|
|---|
| 129 | st2.setLong(1, id);
|
|---|
| 130 | int rowsAffected = st2.executeUpdate();
|
|---|
| 131 |
|
|---|
| 132 | connection.commit();
|
|---|
| 133 |
|
|---|
| 134 | if (rowsAffected > 0) {
|
|---|
| 135 | System.out.println("Faculty with ID " + id + " was deleted successfully!");
|
|---|
| 136 | }
|
|---|
| 137 | } catch (SQLException e) {
|
|---|
| 138 | connection.rollback();
|
|---|
| 139 | throw e;
|
|---|
| 140 | }
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | }
|
|---|