private Doctor update(Doctor object) { Connection connection = connectionManager.getConnection(); PreparedStatement ps = null; try { ps = connection.prepareStatement( "update DRUG d set d.NAME = ?, d.SURNAME = ?, d.EXPERTISE = ? " + " where d.ID = ?"); ps.setString(1, object.getName()); ps.setString(2, object.getSurname()); ps.setString(3, object.getExpertise()); ps.setLong(4, object.getId()); ps.executeUpdate(); return this.find(object.getId()); } catch (SQLException sqle) { sqle.printStackTrace(); } finally { if (ps != null) { try { ps.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); throw new DrugStoreRuntimeException(e); } } } return null; }
public static Doctor buildObjectFromResultSet(ResultSet result, String pointer) throws SQLException { Doctor doctor = new Doctor(); doctor.setId(result.getLong(pointer + ".ID")); doctor.setName(result.getString(pointer + ".NAME")); doctor.setSurname(result.getString(pointer + ".SURNAME")); doctor.setExpertise(result.getString(pointer + ".EXPERTISE")); return doctor; }
@Override public Doctor persist(Doctor object) { if (object.getId() == null) { return this.create(object); } else { return this.update(object); } }
private Doctor create(Doctor object) { Connection connection = connectionManager.getConnection(); PreparedStatement ps = null; try { ps = connection.prepareStatement( "insert into Doctor values (?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); ps.setNull(1, Types.LONGVARCHAR); // auto increment ps.setString(2, object.getName()); ps.setString(3, object.getSurname()); ps.setString(4, object.getExpertise()); ps.executeUpdate(); ResultSet generatedKeys = ps.getGeneratedKeys(); long key = 0; while (generatedKeys.next()) { key = generatedKeys.getLong(1); } return this.find(Long.valueOf(key)); } catch (SQLException sqle) { sqle.printStackTrace(); } finally { if (ps != null) { try { ps.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); throw new DrugStoreRuntimeException(e); } } } return null; }
@Override public void delete(Doctor object) { Connection connection = connectionManager.getConnection(); PreparedStatement ps = null; try { ps = connection.prepareStatement("delete from Doctor where ID = ?"); ps.setLong(1, object.getId()); ps.executeUpdate(); } catch (SQLException sqle) { sqle.printStackTrace(); } finally { if (ps != null) { try { ps.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); throw new DrugStoreRuntimeException(e); } } } }