There is an abstract class DAOFactory, you need to do the following: when creating a DAO, give it a connection, so that the DAO inside can use it so as not to pull the DAOFactory every time, as I have now implemented, DAOFactory daofactory = new DAOFactory (); Naturally, this field should not be here, using the example of code, how to implement all this?
public abstract class DAOFactory implements DAOFactotyInterface { Connection connection = null; public Connection getConnection() throws DAOException { InputStream read = null; try { read = this.getClass().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(read); String dbUrl = properties.getProperty("db.url"); String dbUser = properties.getProperty("db.user"); String dbPassword = properties.getProperty("db.password"); String dbDriver = properties.getProperty("db.driver"); Class.forName(dbDriver); if (connection == null) { connection = DriverManager.getConnection(dbUrl, dbUser, dbPassword); } } catch (IOException e) { throw new DAOException("Properties file is missing ", e); } catch (ClassNotFoundException e) { throw new DAOException("Driver missing ", e); } catch (SQLException e) { throw new DAOException("No connection ", e); } finally { if (read != null) { try { read.close(); } catch (IOException e) { throw new DAOException("InputStream is not closed.", e); } } } return connection; } @Override public ProfileDAO getProfileDAO() { return new MySQLProfileDAO(); } @Override public ImagesDAO getImagesDAO() { return new MySQLImagesDAO(); } @Override public RelationshipsDAO getRelationshipsDAO() { return new MySQLRelationshipsDAO(); } @Override public ScrobblesDAO getScrobblesDAO() { return new MySQLScrobblesDAO(); } } public class MySQLImagesDAO implements ImagesDAO { PreparedStatement preparedStatement = null; Connection connection = null; DAOFactory daofactory = new DAOFactory(); private void getPreparedStatement(String sql) throws DAOException { connection = daofactory.getConnection(); if (preparedStatement == null) { try { preparedStatement = connection.prepareStatement(sql); } catch (Exception e) { throw new DAOException("Get preparedStatment failed.", e); } } } /** * @see ImagesDAO#selectAllImages() */ public List<Images> selectAllImages() throws DAOException { List<Images> allImages = new ArrayList<>(); Statement st = null; ResultSet rs = null; try { connection = daofactory.getConnection(); st = connection.createStatement(); rs = st.executeQuery(PrepStatName.SELECT_IMAGES); while (rs.next()) { Images image = new Images(); image.setId(rs.getInt("id")); image.setIdProfile(rs.getInt("id_profiles")); image.setAvatar(rs.getString("avatar")); allImages.add(image); } } catch (Exception e) { throw new DAOException("List of images is not selected.", e); } finally { try { if (st != null) { st.close(); } if (rs != null) { rs.close(); } } catch (Exception e) { throw new DAOException("The ResultSet/Statement is not closed.", e); } } return allImages; } /** * @see ImagesDAO#updateImages(Images) */ public void updateImages(Images images) throws DAOException { try { getPreparedStatement(PrepStatName.UPDATE_IMAGES); preparedStatement.setInt(1, images.getIdProfile()); preparedStatement.setString(2, images.getAvatar()); preparedStatement.setInt(3, images.getId()); preparedStatement.executeUpdate(); } catch (Exception e) { throw new DAOException("The images is not updated.", e); } } /** * @see ImagesDAO#deleteImages(Images) */ public void deleteImages(Images images) throws DAOException { try { getPreparedStatement(PrepStatName.DEL_IMAGES); preparedStatement.setInt(1, images.getId()); preparedStatement.executeUpdate(); } catch (Exception e) { throw new DAOException("The images is not delete.", e); } } /** * @see ImagesDAO#insertImages(Images) */ public void insertImages(Images images) throws DAOException { try { getPreparedStatement(PrepStatName.INSERT_IMAGES); preparedStatement.setInt(1, images.getIdProfile()); preparedStatement.setString(2, images.getAvatar()); preparedStatement.executeUpdate(); } catch (Exception e) { throw new DAOException("The images is not create.", e); } } public void close() throws DAOException { try { if (preparedStatement != null) { preparedStatement.close(); preparedStatement = null; } } catch (Exception e) { throw new DAOException("The preparedStatement is not closed. ", e); } try { if (connection != null) { connection.close(); connection = null; } } catch (Exception e) { throw new DAOException("The connection is not closed.", e); } } }
@Override public ImagesDAO getImagesDAO() { return new MySQLImagesDAO(); }@Override public ImagesDAO getImagesDAO() { return new MySQLImagesDAO(); }pass the connection to the DAO to accept it in theprivate void getPreparedStatement(String sql) throws DAOException { }method.private void getPreparedStatement(String sql) throws DAOException { }i.e. the task is: “when creating a dao, give him a connection, so that tao inside can use it, and not pull the factor every time” is not sure what it is in these places of the code that is implemented, therefore he posted a full listing of classes - PolkovnikJ