FileDocCategorySizeDatePackage
PatientDatabaseDAO.javaAPI DocExample2515Mon Nov 24 10:29:48 GMT 2003com.oreilly.patterns.chapter8

PatientDatabaseDAO

public class PatientDatabaseDAO extends Object implements PatientDAO

Fields Summary
Constructors Summary
Methods Summary
public com.oreilly.patterns.chapter7.PatientDTOcreatePatient(com.oreilly.patterns.chapter7.PatientDTO patient)

    // Creation code goes here
    return null;
  
public com.oreilly.patterns.chapter7.PatientDTOfindPatient(long pat_no)

  
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    PatientDTO patient = null;
    
    try {
      con = getConnection(); // local method for JNDI Lookup
      ps = con.prepareStatement("select * from patient where pat_no = ?");
      ps.setLong(1, pat_no);
      rs = ps.executeQuery();

      if(!rs.next())
       return null;

      patient = new PatientDTO();    
      patient.pat_no = pat_no;
      patient.fname = rs.getString("fname");
      patient.lname= rs.getString("lname");
      ps.close();
      rs.close();
      
      ps = con.prepareStatement(
        "select * from patient_address where pat_no = ? and address_label in " +
        "('Home', 'Work')");
        
      ps.setLong(1, pat_no);
      rs = ps.executeQuery();
      // Load any work or home
      while(rs.next()) {
        String addrType = rs.getString("ADDRESS_LABEL");
        Address addr = new Address();
        addr.addressType = addrType;
        addr.address1 = rs.getString("addr1");
        addr.address2 = rs.getString("addr2");
        addr.city = rs.getString("city");
        addr.province = rs.getString("province");
        addr.postcode = rs.getString("postcode");
        patient.addresses.add(addr);
      }
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      try {//TODO
        if(rs != null) rs.close();
        if(ps != null) ps.close();
        if(con != null) con.close();
      } catch (SQLException e) {}
    }
    
    return patient;
  
private java.sql.ConnectiongetConnection()

    try {
      Context jndiContext = new InitialContext();
      DataSource ds = (DataSource)jndiContext.lookup("java:comp/env/jdbc/DataChapterDS");
      return ds.getConnection();
    } catch (NamingException ne) {
        throw new SQLException (ne.getMessage());
    }
  
public voidsavePatient(com.oreilly.patterns.chapter7.PatientDTO patient)

    // Persistence code goes here