There are three methods that differ literally in a couple of lines. Can I somehow avoid duplicate code?
@Override public List<Employee> findAll() { List<Employee> list = new ArrayList<>(); Connection connection = null; try { connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL_FIND_ALL); ResultSet rs = statement.executeQuery(); while (rs.next()) { Employee tempEmployee = convertRowToEmployee(rs); list.add(tempEmployee); } } catch (SQLException e) { e.printStackTrace(); } finally { dataSource.closeConnection(connection); } return list; } @Override public List<Employee> findByName(String name) { List<Employee> list = new ArrayList<>(); Connection connection = null; try { connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL_FIND_BY_NAME); statement.setString(1, name); ResultSet rs = statement.executeQuery(); while (rs.next()) { Employee tempEmployee = convertRowToEmployee(rs); list.add(tempEmployee); } } catch (SQLException e) { e.printStackTrace(); } finally { dataSource.closeConnection(connection); } return list; } @Override public List<Employee> findAllAbsenceEmployeeByDate(Date startDate, Date endDate) { List<Employee> list = new ArrayList<>(); Connection connection = null; try { connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL_FIND_ALL_ABSENCE_BY_DATE); statement.setDate(1, startDate); statement.setDate(2, endDate); ResultSet rs = statement.executeQuery(); while (rs.next()) { Employee tempEmployee = convertRowToEmployee(rs); list.add(tempEmployee); } } catch (SQLException e) { e.printStackTrace(); } finally { dataSource.closeConnection(connection); } return list; } Converting rows into an object rendered into a separate method convertRowToEmployee (rs), merging and closing rendered into a separate class DataSource