@Override public Doctor find(Long key) { Connection connection = connectionManager.getConnection(); PreparedStatement ps = null; ResultSet result = null; Doctor doctor = null; try { ps = connection.prepareStatement("select * from Doctor d where d.id = ?"); ps.setLong(1, key); result = ps.executeQuery(); while (result.next()) { doctor = DoctorRepositoryJDBCImpl.buildObjectFromResultSet(result, "d"); } } catch (SQLException sqle) { sqle.printStackTrace(); } finally { if (result != null) { try { result.close(); ps.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); throw new DrugStoreRuntimeException(e); } } } return doctor; }
@Override public List<Doctor> listAll(String orderClause) { Connection connection = connectionManager.getConnection(); PreparedStatement ps = null; ResultSet result = null; List<Doctor> resultList = new ArrayList<Doctor>(); try { StringBuffer sqlBuffer = new StringBuffer("select * from Doctor d "); if (StringUtil.isEmpty(orderClause)) { sqlBuffer.append("order by d.name asc"); } else { sqlBuffer.append(orderClause); } ps = connection.prepareStatement(sqlBuffer.toString()); result = ps.executeQuery(); while (result.next()) { Doctor doctor = DoctorRepositoryJDBCImpl.buildObjectFromResultSet(result, "d"); resultList.add(doctor); } } catch (SQLException sqle) { sqle.printStackTrace(); } finally { if (result != null) { try { result.close(); ps.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); throw new DrugStoreRuntimeException(e); } } } return resultList; }