Code example:

class StudentDAO extends AbstractDAO <Integer, Student> { private static final String SQL_SELECT_ALL_STUDENT = "SELECT ID, FIRST_NAME, SECOND_NAME, BIRTH_DATE,ENTER_YEAR FROM STUDENT"; private static final String SQL_SELECT_STUDENT_BY_ID = "SELECT FIRST_NAME, SECOND_NAME, BIRTH_DATE,ENTER_YEAR FROM STUDENT WHERE ID=?"; private static final String SQL_SELECT_STUDENT_BY_LASTNAME ="SELECT id,FIRST_NAME,BIRTH_DATE,ENTER_YEAR FROM. STUDENT WHERE SECOND_NAME=?"; private static final String SQL_DELETE_STUDENT_BY_ID = "DELETE FROM STUDENT WHERE ID =?"; private static final String SQL_INSERT_STUDENT = "INSERT INTO STUDENT"+"(FIRST_NAME, SECOND_NAME, BIRTH_DATE, ENTER_YEAR)"+"VALUES (?,?,?,?)"; private PreparedStatement preparedStatement1; private PreparedStatement preparedStatementSelectById; private PreparedStatement preparedStatementfindAll; private PreparedStatement preparedStatementDeleteStudentById; private PreparedStatement preparedStatementInsertStudent; public StudentDAO(Connection connection) throws DAOException, SQLException { super(connection); try { preparedStatementInsertStudent =connection.prepareStatement(SQL_INSERT_STUDENT); preparedStatement1 = connection.prepareStatement(SQL_SELECT_STUDENT_BY_LASTNAME); preparedStatementfindAll = connection.prepareStatement(SQL_SELECT_ALL_STUDENT); preparedStatementDeleteStudentById = connection.prepareStatement(SQL_DELETE_STUDENT_BY_ID); preparedStatementSelectById = connection.prepareStatement(SQL_SELECT_STUDENT_BY_ID); } catch (Exception e) { throw new DAOException(e); } } } 

Difficulties arose in how to close ps when using them in the class constructor?

  • @ a.chugunov Then how is it not always to use ps when using its tools when calling the dao class method.? - Evgeni
  • Describe in the question what difficulties you have when closing PS - a.chugunov
  • Since when did the "deer begin to walk in the steppe"? So do you, sir, how do you generally initialize connections in the constructor? You can not write separate functions with the initialization of the objects and their closure? - GenCloud

0