/
ihaverak
/
persons
Обзор
Документация
Войти
/
ihaverak
/
persons
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
dev5
src/main/java/dao/StudentDbDAO.java
128 строк
5 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 domain.Student; import exception.DAOException; public class StudentDbDAO implements RepositoryDAO<Student> { private static final String SELECT_ALL = "SELECT id, groupid, firstname, lastname, phone, email FROM students ORDER BY lastname ASC"; private static final String SELECT_BY_ID = "SELECT id, groupid, firstname, lastname, phone, email FROM students WHERE id = ?"; private static final String INSERT = "INSERT INTO students(groupid, firstname, lastname, phone, email) VALUES(?,?,?,?,?)"; private static final String EDIT = "UPDATE students SET groupid = ?, firstname = ?, lastname = ?, phone = ?, email = ? WHERE id = ?"; private static final String DELETE = "DELETE FROM students WHERE id = ?"; private ConnectionBuilder builder = new DbConnectionBuilder(); private GroupDbDAO groupDao = new GroupDbDAO(); private Connection getConnection() throws SQLException { return builder.getConnection(); } @Override public Long insert(Student student) throws DAOException { try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(INSERT, new String[]{"id"})) { Long id = -1L; pst.setLong(1, student.getIdGroup()); pst.setString(2, student.getFirstName()); pst.setString(3, student.getLastName()); pst.setString(4, student.getPhone()); pst.setString(5, student.getEmail()); 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(Student student) throws DAOException { try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(EDIT)) { pst.setLong(1, student.getIdGroup()); pst.setString(2, student.getFirstName()); pst.setString(3, student.getLastName()); pst.setString(4, student.getPhone()); pst.setString(5, student.getEmail()); pst.setLong(6, student.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 Student findById(Long id) throws DAOException { Student student = null; try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(SELECT_BY_ID)) { pst.setLong(1, id); ResultSet rs = pst.executeQuery(); if (rs.next()) { student = fillStudent(rs); } rs.close(); } catch (Exception e) { throw new DAOException(e); } return student; } @Override public List<Student> findAll() throws DAOException { List<Student> list = new LinkedList<>(); try (Connection con = getConnection(); PreparedStatement pst = con.prepareStatement(SELECT_ALL); ResultSet rs = pst.executeQuery()) { List<Group> groups = groupDao.findAll(); while (rs.next()) { Student student = fillStudent(rs); Long idGroup = rs.getLong("groupid"); student.setGroup(groupDao.findById(idGroup, groups)); list.add(student); } } catch (Exception e) { throw new DAOException(e); } return list; } private Student fillStudent(ResultSet rs) throws SQLException { Student student = new Student(); student.setId(rs.getLong("id")); student.setFirstName(rs.getString("firstname")); student.setLastName(rs.getString("lastname")); student.setPhone(rs.getString("phone")); student.setEmail(rs.getString("email")); student.setIdGroup(rs.getLong("groupid")); return student; } }