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; }
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; }