FileDocCategorySizeDatePackage
MemberSerializedDAO.javaAPI DocExample2714Mon Nov 24 10:29:48 GMT 2003com.oreilly.patterns.chapter8

MemberSerializedDAO

public class MemberSerializedDAO extends Object

Fields Summary
Constructors Summary
public MemberSerializedDAO()

  
Methods Summary
public MemberDTOfindMember(long member_no)

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    MemberDTO member = null;
    
    try {
      con = getConnection();
      ps = con.prepareStatement(
        "select object_data from members where member_no = ?"); 
      ps.setLong(1, member_no);
      rs = ps.executeQuery();
      if(rs.next()) {
        ObjectInputStream ois = new ObjectInputStream(rs.getBinaryStream(1));
        member = (MemberDTO)ois.readObject();
        ois.close();
      }
      rs.close();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      if (ps != null)
        try { ps.close(); } catch (SQLException e) {}
      if (con != null)
        try { con.close(); } catch (SQLException e) {}
    }
    
    return member;
  
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 voidsaveMember(MemberDTO member)

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    long memberNo = member.getMemberNumber();
    if(memberNo < 1) 
      return;
   
    try {
      con = getConnection();
      ps = con.prepareStatement("delete from members where member_no = ?");
      ps.setLong(1, memberNo);
      ps.executeUpdate();
      ps.close();

      ps = con.prepareStatement("insert into members " +
        "(member_no, object_data) values (?, ?)");
      ps.setLong(1, memberNo);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      ObjectOutputStream oos = new ObjectOutputStream(baos);
      oos.writeObject(member);
      ps.setBytes(2, baos.toByteArray());
      ps.executeUpdate();
      
    } catch (IOException e) {
      e.printStackTrace();
    } catch (SQLException e) {
      e.printStackTrace();
    } finally {
      if(ps != null) 
        try { ps.close(); } catch (SQLException e) {}
      if(con != null)
        try { con.close(); } catch (SQLException e) {}
    }