/
ihaverak
/
persons
Обзор
Документация
Войти
/
ihaverak
/
persons
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev6
src/main/java/dao/GroupDbDAO.java
114 строк
4 KB
ihaverak
лаба 5
23 май 2026, 00:56
23 май 2026, 00:56
4bb74aa
Код
Авторство
О чём код?
package dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.LinkedList; import java.util.List; import domain.Group; import exception.DAOException; public class GroupDbDAO implements RepositoryDAO<Group> { private static final String SELECT_ALL = "SELECT id, groupname FROM groups ORDER BY groupname ASC"; private static final String SELECT_BY_ID = "SELECT id, groupname FROM groups WHERE id = ?"; private static final String INSERT = "INSERT INTO groups(groupname) VALUES(?)"; private static final String EDIT = "UPDATE groups SET groupname = ? WHERE id = ?"; private static final String DELETE = "DELETE FROM groups WHERE id = ?"; private ConnectionBuilder builder = new DbConnectionBuilder(); private Connection getConnection() throws SQLException { return builder.getConnection(); } @Override public Long insert(Group group) throws DAOException { try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(INSERT, new String[]{"id"})) { Long id = -1L; pst.setString(1, group.getNameGroup()); pst.executeUpdate(); ResultSet gk = pst.getGeneratedKeys(); if (gk.next()) { id = gk.getLong("id"); } gk.close(); return id; } catch (Exception e) { throw new DAOException(e); } } @Override public void update(Group group) throws DAOException { try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(EDIT)) { pst.setString(1, group.getNameGroup()); pst.setLong(2, group.getId()); pst.executeUpdate(); } catch (Exception e) { throw new DAOException(e); } } @Override public void delete(Long id) throws DAOException { try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(DELETE)) { pst.setLong(1, id); pst.executeUpdate(); } catch (Exception e) { throw new DAOException(e); } } @Override public Group findById(Long id) throws DAOException { Group group = null; try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(SELECT_BY_ID)) { pst.setLong(1, id); ResultSet rs = pst.executeQuery(); if (rs.next()) { group = new Group(); group.setId(rs.getLong("id")); group.setNameGroup(rs.getString("groupname")); } rs.close(); } catch (Exception e) { throw new DAOException(e); } return group; } @Override public List<Group> findAll() throws DAOException { List<Group> list = new LinkedList<>(); try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(SELECT_ALL); ResultSet rs = pst.executeQuery()) { while (rs.next()) { Group group = new Group(); group.setId(rs.getLong("id")); group.setNameGroup(rs.getString("groupname")); list.add(group); } } catch (Exception e) { throw new DAOException(e); } return list; } public Group findById(Long id, List<Group> groups) { if (groups != null) { for (Group g : groups) { if (g.getId().equals(id)) { return g; } } } return null; } }