h42442
s 00002/00002/00190
d D 1.3 99/09/01 10:23:34 monicap 4 3
c 
e
s 00007/00001/00185
d D 1.2 99/08/31 10:50:06 monicap 3 1
c 
e
s 00000/00000/00000
d R 1.2 99/06/02 12:08:30 Codemgr 2 1
c SunPro Code Manager data about conflicts, renames, etc...
c Name history : 1 0 developer/onlineTraining/Programming/JDCBook/Code/registration/JDBC/RegistrationBean.java
e
s 00186/00000/00000
d D 1.1 99/06/02 12:08:29 monicap 1 0
c date and time created 99/06/02 12:08:29 by monicap
e
u
U
f e 0
t
T
I 1
package registration;

import java.rmi.RemoteException;
import javax.ejb.*;
import java.util.*;
import java.text.NumberFormat;
import java.sql.*;

// Bean-managed persistence using JDBC[TM]

public class RegistrationBean implements EntityBean {

    private transient EntityContext ctx;
I 4
    private PreparedStatement ps=null;
    private Connection con=null;
E 4
    public String theuser, password, creditcard, emailaddress;
    public double balance;

    static {
        new weblogic.jdbc.jts.Driver(); 
    }

    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection("jdbc:weblogic:jts:ejbPool");
    }



    public boolean verifyPassword(String password) throws RemoteException { 
	if (this.password.equals(password)) {
	    return true;
	} else {
            return false;
        }
    }
    public String getEmailAddress() throws RemoteException { 
	return emailaddress;
    }
    public String getUser() throws RemoteException { 
	return theuser;
    }
    public int adjustAccount(double amount) throws RemoteException { 
        balance=balance+amount;
        return(0);
    }

    public double getBalance() throws RemoteException {
	return balance;
    }

    public RegistrationPK ejbCreate(String theuser, String password, String emailaddress, String creditcard) throws CreateException, RemoteException {

	this.theuser=theuser;
	this.password=password;
	this.emailaddress=emailaddress;
	this.creditcard=creditcard;
	this.balance=0;

D 4
        Connection con = null;
        PreparedStatement ps = null;;
E 4
        try {
           con=getConnection();
           ps=con.prepareStatement("insert into registration (theuser, password, emailaddress, creditcard, balance) values (?, ?, ?, ?, ?)");
           ps.setString(1, theuser);
           ps.setString(2, password);
           ps.setString(3, emailaddress);
           ps.setString(4, creditcard);
           ps.setDouble(5, balance);
           if (ps.executeUpdate() != 1) {
               throw new CreateException ("JDBC did not create any row");
           }
           RegistrationPK primaryKey = new RegistrationPK();
           primaryKey.theuser = theuser;
           return primaryKey;
       } catch (CreateException ce) {
          throw ce;
       } catch (SQLException sqe) {
          throw new CreateException (sqe.getMessage());
       } finally {
          try {
             ps.close();
I 3
          } catch (Exception ignore) {}
          try {
E 3
             con.close();
          } catch (Exception ignore) {}
       }
    }
 

    public void ejbPostCreate(String theuser, String password, String emailaddress, String creditcard) throws CreateException, RemoteException {
    }

    public void setEntityContext(javax.ejb.EntityContext ctx) throws RemoteException {
    this.ctx = ctx;
    }

    public void unsetEntityContext() throws RemoteException {   
    ctx = null;  
    } 

    public void ejbRemove() throws RemoteException, RemoveException { }
    public void ejbActivate() throws RemoteException { }
    public void ejbPassivate() throws RemoteException { }
    public void ejbLoad() throws RemoteException { 
       try {
          refresh((RegistrationPK) ctx.getPrimaryKey());
       }
       catch (FinderException fe) {
          throw new RemoteException (fe.getMessage());
       }
    }

    public void ejbStore() throws RemoteException { 
        Connection con = null;
        PreparedStatement ps = null;
        try {
           con = getConnection();
           ps = con.prepareStatement("update registration set password = ?, emailaddress = ?, creditcard = ?, balance = ? where theuser = ?");
           ps.setString(1, password);
           ps.setString(2, emailaddress);
           ps.setString(3, creditcard);
           ps.setDouble(4, balance);
           ps.setString(5, theuser);
           int i = ps.executeUpdate();
           if (i == 0) {
               throw new RemoteException ("ejbStore: Registration (" + theuser
                                   + ") not updated");
           }
       } catch (RemoteException re) {
           throw re;
       } catch (SQLException sqe) {
          throw new RemoteException (sqe.getMessage());
       } finally {
          try {
             ps.close();
I 3
          } catch (Exception ignore) {}
          try {
E 3
             con.close();
          } catch (Exception ignore) {}
       }
    }

    public RegistrationPK ejbFindByPrimaryKey(RegistrationPK pk)
                           throws FinderException, RemoteException {

        if ((pk == null) || (pk.theuser == null)) {
            throw new FinderException ("primary key cannot be null");
        }
        refresh(pk);
        return pk;
    }

    private void refresh(RegistrationPK pk) 
                      throws FinderException, RemoteException {

        if (pk == null) {
            throw new RemoteException ("primary key cannot be null");
        }
        Connection con = null;
        PreparedStatement ps = null;
        try {
           con=getConnection();
           ps=con.prepareStatement("select password, emailaddress, creditcard, balance from registration where theuser = ?");
           ps.setString(1, pk.theuser);
           ps.executeQuery();
           ResultSet rs = ps.getResultSet();
           if (rs.next()) {
              theuser = pk.theuser;
              password = rs.getString(1);
              emailaddress = rs.getString(2);
              creditcard = rs.getString(3);
              balance = rs.getDouble(4);
           }
           else {
               throw new FinderException ("Refresh: Registration ("
                                   + pk.theuser + ") not found");
           }
       }
       catch (SQLException sqe) {
           throw new RemoteException (sqe.getMessage());
       }
       finally {
          try {
             ps.close();
D 3
             con.close();
E 3
          }
          catch (Exception ignore) {}
I 3
          try {
             con.close();
          } catch (Exception ignore) {}
E 3
       }
    }

}

E 1
