| 1 | package DAO.impl;
|
|---|
| 2 |
|
|---|
| 3 | import DAO.JDBCUtils;
|
|---|
| 4 | import DAO.UniversityDAO;
|
|---|
| 5 | import model.University;
|
|---|
| 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 |
|
|---|
| 14 | public class UniversityDAOImpl implements UniversityDAO {
|
|---|
| 15 | @Override
|
|---|
| 16 | public List<University> getAll() throws SQLException {
|
|---|
| 17 | final String SELECT_ALL_UNIVERSITY = "SELECT * FROM university;";
|
|---|
| 18 |
|
|---|
| 19 | List<University> universityList = new ArrayList<>();
|
|---|
| 20 |
|
|---|
| 21 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 22 | PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_UNIVERSITY)) {
|
|---|
| 23 | ResultSet rs = preparedStatement.executeQuery();
|
|---|
| 24 | while (rs.next()) {
|
|---|
| 25 | long id = rs.getLong("id");
|
|---|
| 26 | String name = rs.getString("name");
|
|---|
| 27 | String location = rs.getString("location");
|
|---|
| 28 | Boolean IsPrivate = rs.getBoolean("IsPrivate");
|
|---|
| 29 |
|
|---|
| 30 | System.out.println(id + ", " + name + ", " + location + ", " + IsPrivate);
|
|---|
| 31 |
|
|---|
| 32 | University university = new University(id, name, location, IsPrivate);
|
|---|
| 33 | universityList.add(university);
|
|---|
| 34 | }
|
|---|
| 35 | } catch (SQLException e) {
|
|---|
| 36 | System.err.println("Error updating Faculty: " + e.getMessage());
|
|---|
| 37 | throw e;
|
|---|
| 38 | //JDBCUtils.printSQLException(e);
|
|---|
| 39 | }
|
|---|
| 40 | return universityList;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | @Override
|
|---|
| 44 | public List<University> getUniversitiesInSkopje() throws SQLException {
|
|---|
| 45 | final String SELECT_IN_SKOPJE = "SELECT * FROM University WHERE location = 'Skopje, MK';";
|
|---|
| 46 | List<University> universities = new ArrayList<>();
|
|---|
| 47 |
|
|---|
| 48 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 49 | PreparedStatement preparedStatement = connection.prepareStatement(SELECT_IN_SKOPJE)) {
|
|---|
| 50 | ResultSet rs = preparedStatement.executeQuery();
|
|---|
| 51 | while (rs.next()) {
|
|---|
| 52 | long id = rs.getLong("id");
|
|---|
| 53 | String name = rs.getString("name");
|
|---|
| 54 | String location = rs.getString("location");
|
|---|
| 55 | Boolean IsPrivate = rs.getBoolean("IsPrivate");
|
|---|
| 56 |
|
|---|
| 57 | universities.add(new University(id, name, location, IsPrivate));
|
|---|
| 58 | }
|
|---|
| 59 | } catch (SQLException e) {
|
|---|
| 60 | System.err.println("Error fetching universities: " + e.getMessage());
|
|---|
| 61 | throw e;
|
|---|
| 62 | }
|
|---|
| 63 | return universities;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | @Override
|
|---|
| 67 | public void update(University university) throws SQLException {
|
|---|
| 68 | final String UPDATE_UNIVERSITY_SQL = "UPDATE university SET name = ?, location = ?, IsPrivate = ? WHERE id = ?;";
|
|---|
| 69 |
|
|---|
| 70 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 71 | PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_UNIVERSITY_SQL)) {
|
|---|
| 72 | preparedStatement.setString(1, university.getName());
|
|---|
| 73 | preparedStatement.setString(2, university.getLocation());
|
|---|
| 74 | preparedStatement.setBoolean(3, university.getIsPrivate()); preparedStatement.setLong(4, university.getId());
|
|---|
| 75 | preparedStatement.executeUpdate();
|
|---|
| 76 | } catch (SQLException e) {
|
|---|
| 77 | JDBCUtils.printSQLException(e);
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | @Override
|
|---|
| 83 | public University save(University university) throws SQLException {
|
|---|
| 84 | final String INSERT_UNIVERSITY_SQL = "INSERT INTO university (name, location, IsPrivate) VALUES (?, ?, ?);";
|
|---|
| 85 | University savedUniversity = null;
|
|---|
| 86 |
|
|---|
| 87 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 88 | PreparedStatement preparedStatement = connection.prepareStatement(INSERT_UNIVERSITY_SQL, PreparedStatement.RETURN_GENERATED_KEYS)) {
|
|---|
| 89 |
|
|---|
| 90 | preparedStatement.setString(1, university.getName());
|
|---|
| 91 | preparedStatement.setString(2, university.getLocation());
|
|---|
| 92 | preparedStatement.setBoolean(3, university.getIsPrivate());
|
|---|
| 93 | int affectedRows = preparedStatement.executeUpdate();
|
|---|
| 94 |
|
|---|
| 95 | if (affectedRows == 0) {
|
|---|
| 96 | throw new SQLException("Creating university failed, no rows affected.");
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | try (ResultSet generatedKeys = preparedStatement.getGeneratedKeys()) {
|
|---|
| 100 | if (generatedKeys.next()) {
|
|---|
| 101 | long generatedId = generatedKeys.getLong(1);
|
|---|
| 102 | savedUniversity = new University(generatedId, university.getName(), university.getLocation(), university.getIsPrivate());
|
|---|
| 103 | } else {
|
|---|
| 104 | throw new SQLException("Creating university failed, no ID obtained.");
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | } catch (SQLException e) {
|
|---|
| 108 | System.err.println("Error saving University: " + e.getMessage());
|
|---|
| 109 | throw e;
|
|---|
| 110 | }
|
|---|
| 111 | return savedUniversity;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | @Override
|
|---|
| 115 | public University getByID(Long id) throws SQLException {
|
|---|
| 116 | final String SELECT_BY_ID = "SELECT * FROM university WHERE id = ?;";
|
|---|
| 117 | University university = null;
|
|---|
| 118 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 119 | PreparedStatement preparedStatement = connection.prepareStatement(SELECT_BY_ID)) {
|
|---|
| 120 |
|
|---|
| 121 | preparedStatement.setLong(1, id);
|
|---|
| 122 | ResultSet rs = preparedStatement.executeQuery();
|
|---|
| 123 |
|
|---|
| 124 | if (rs.next()) {
|
|---|
| 125 | String name = rs.getString("name");
|
|---|
| 126 | String location = rs.getString("location");
|
|---|
| 127 | Boolean isPrivate = rs.getBoolean("IsPrivate");
|
|---|
| 128 | university = new University(id, name, location, isPrivate);
|
|---|
| 129 | System.out.println("Fetched: " + university);
|
|---|
| 130 | } else {
|
|---|
| 131 | System.out.println("No University found with ID " + id);
|
|---|
| 132 | }
|
|---|
| 133 | } catch (SQLException e) {
|
|---|
| 134 | e.printStackTrace();
|
|---|
| 135 | throw e;
|
|---|
| 136 | }
|
|---|
| 137 | return university;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 | @Override
|
|---|
| 142 | public void delete(Long id) throws SQLException {
|
|---|
| 143 | final String DELETE_UNIVERSITY_SQL = "DELETE FROM university WHERE id = ?;";
|
|---|
| 144 |
|
|---|
| 145 | try (Connection connection = JDBCUtils.getConnection();
|
|---|
| 146 | PreparedStatement preparedStatement = connection.prepareStatement(DELETE_UNIVERSITY_SQL)) {
|
|---|
| 147 | preparedStatement.setLong(1, id);
|
|---|
| 148 | int rowsAffected = preparedStatement.executeUpdate();
|
|---|
| 149 | if (rowsAffected > 0) {
|
|---|
| 150 | System.out.println("University with ID " + id + " was deleted successfully!");
|
|---|
| 151 | } else {
|
|---|
| 152 | System.out.println("No University found with ID " + id);
|
|---|
| 153 | }
|
|---|
| 154 | } catch (SQLException e) {
|
|---|
| 155 | System.err.println("Error updating University: " + e.getMessage());
|
|---|
| 156 | throw e;
|
|---|
| 157 | //JDBCUtils.printSQLException(e);
|
|---|
| 158 | }
|
|---|
| 159 | }
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|