/** * Changes a patient in the database * * @param p The Patient object containing the new data (should have the same ID as the patient to * replace) */ public void updatePatient(Patient p) { try { statement = con.prepareStatement( "UPDATE patients SET firstname=?, lastname=?, preferredname=?, volunteer=?, " + "gender=?, notes=?, clinic=?, availability=?, myoscar_verified=?, alerts=?, volunteer2=? WHERE patient_ID=?"); statement.setString(1, p.getFirstName()); statement.setString(2, p.getLastName()); statement.setString(3, p.getPreferredName()); statement.setInt(4, p.getVolunteer()); statement.setString(5, p.getGender()); statement.setString(6, p.getNotes()); statement.setString(7, p.getClinic()); statement.setString(8, p.getAvailability()); statement.setString(9, p.getMyoscarVerified()); statement.setString(10, p.getAlerts()); statement.setInt(11, p.getPartner()); statement.setInt(12, p.getPatientID()); statement.execute(); } catch (SQLException e) { System.out.println("Error: Could not update patient"); e.printStackTrace(); } finally { try { statement.close(); } catch (Exception e) { // Ignore } } }
/** * Set up volunteer's displayName in patient object * * @param p patient * @param who volunteer1 or volunteer2 in patient */ private void setVolunteerDisplayName(Patient p, String who) { try { statement = con.prepareStatement("SELECT firstname, lastname FROM volunteers WHERE volunteer_ID=?"); if (who.equals("volunteer1")) statement.setInt(1, p.getVolunteer()); else statement.setInt(1, p.getPartner()); ResultSet rs = statement.executeQuery(); while (rs.next()) { StringBuilder sb = new StringBuilder(); if (!Utils.isNullOrEmpty(rs.getString("firstname"))) { sb.append(rs.getString("firstname")); sb.append(" "); } if (rs.getString("lastname") != null) { sb.append(rs.getString("lastname")); } if (who.equals("volunteer1")) { if (!Utils.isNullOrEmpty(sb.toString())) p.setVolunteerName(sb.toString()); else p.setVolunteerName(""); } else { if (!Utils.isNullOrEmpty(sb.toString())) p.setPartnerName(sb.toString()); else p.setPartnerName(""); } } } catch (SQLException e) { System.out.println("Error: Failed to create Patient object"); e.printStackTrace(); } }
/** * Saves a patient in the database * * @param p The Patient object to save */ public void createPatient(Patient p) { // check if it is new record in DB if (!isExist(p)) { try { statement = con.prepareStatement( "INSERT INTO patients (firstname, lastname, preferredname, volunteer," + " gender, notes, volunteer2, alerts, myoscar_verified, clinic, availability) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); statement.setString(1, p.getFirstName()); statement.setString(2, p.getLastName()); statement.setString(3, p.getPreferredName()); statement.setInt(4, p.getVolunteer()); statement.setString(5, p.getGender()); statement.setString(6, p.getNotes()); statement.setInt(7, p.getPartner()); statement.setString(8, p.getAlerts()); statement.setString(9, p.getMyoscarVerified()); statement.setString(10, p.getClinic()); statement.setString(11, p.getAvailability()); statement.execute(); } catch (SQLException e) { System.out.println("Error: Could not create patient"); e.printStackTrace(); } finally { try { statement.close(); } catch (Exception e) { // Ignore } } } }