/
Maksim_Shchelochok
/
Course_Work
Обзор
Документация
Войти
/
Maksim_Shchelochok
/
Course_Work
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/model/SpecialityManager.java
95 строк
4 KB
Maksim
Исправление орфографических ошибок
26 июн 2026, 02:27
26 июн 2026, 02:27
e1279cb
Код
Авторство
О чём код?
package model; import util.ConnectDatabase; import util.Validator; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class SpecialityManager { private Connection getConnection() { return ConnectDatabase.getInstance().getConnection(); } // Специальности вместе с названием направления public List<Speciality> getAll() { List<Speciality> list = new ArrayList<>(); String sql = "SELECT s.specialty_id, s.name, s.direction_id, d.name AS direction_name " + "FROM specialties s " + "JOIN directions d ON s.direction_id = d.direction_id " + "ORDER BY s.specialty_id"; try (PreparedStatement ps = getConnection().prepareStatement(sql); ResultSet rs = ps.executeQuery()) { while (rs.next()) { list.add(new Speciality( rs.getInt("specialty_id"), rs.getString("name"), rs.getInt("direction_id"), rs.getString("direction_name"))); } } catch (SQLException e) { System.out.println("Ошибка чтения специальностей: " + e.getMessage()); } return list; } public List<Direction> getAllDirections() { List<Direction> list = new ArrayList<>(); String sql = "SELECT direction_id, name FROM directions ORDER BY name"; try (PreparedStatement ps = getConnection().prepareStatement(sql); ResultSet rs = ps.executeQuery()) { while (rs.next()) { list.add(new Direction(rs.getInt("direction_id"), rs.getString("name"))); } } catch (SQLException e) { System.out.println("Ошибка чтения направлений: " + e.getMessage()); } return list; } public String create(String name, int directionId) { if (!Validator.isValidTitle(name)) { return "Название должно содержать минимум 2 символа."; } String sql = "INSERT INTO specialties (name, direction_id) VALUES (?, ?)"; try (PreparedStatement ps = getConnection().prepareStatement(sql)) { ps.setString(1, name.trim()); ps.setInt(2, directionId); ps.executeUpdate(); return null; } catch (SQLException e) { return "Ошибка добавления: " + e.getMessage(); } } public String update(int id, String name, int directionId) { if (!Validator.isValidTitle(name)) { return "Название должно содержать минимум 2 символа."; } String sql = "UPDATE specialties SET name = ?, direction_id = ? WHERE specialty_id = ?"; try (PreparedStatement ps = getConnection().prepareStatement(sql)) { ps.setString(1, name.trim()); ps.setInt(2, directionId); ps.setInt(3, id); ps.executeUpdate(); return null; } catch (SQLException e) { return "Ошибка изменения: " + e.getMessage(); } } public String delete(int id) { String sql = "DELETE FROM specialties WHERE specialty_id = ?"; try (PreparedStatement ps = getConnection().prepareStatement(sql)) { ps.setInt(1, id); ps.executeUpdate(); return null; } catch (SQLException e) { return "Не удалось удалить: " + e.getMessage(); } } }