Esta sección nos muestra cómo convertir la clase RegistrationBean.java para acceder a la base de datos con la clase PreparedStatement del JDBC.
El método getConnection necesita la clase estática DriverManager para un motor de la base datos registrada que corresponda con la URL. En este caso, la URL es weblogic.jdbc.jts.Driver.
//Create static instance of database driver
static {
new weblogic.jdbc.jts.Driver();
}
//Get registered driver from static instance
public Connection getConnection() throws SQLException{
return DriverManager.getConnection(
"jdbc:weblogic:jts:ejbPool");
}
Un objeto PreparedStatement se crea desde una sentenica SQL que se envía a la base de datos y se precompila antes de enviar cualquier dato. Podemos llamar a las sentencias setXXX apropiadas sobre el objeto PreparedStatement para enviar datos. Manteniendo los objetos PreparedStatement y Connection como variables de ejemplar privadas reducimos la sobrecarga porque las sentencias SQL no tienen que compilarse cada vez que se envían.
Lo último que hace el método ejbCreate es crear una clase de clave primaria con el ID del usuario, y devolverlo al contenedor.
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;
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 a 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();
} catch (Exception ignore) {}
try {
con.close();
} catch (Exception ignore) {}
}
}
public void ejbLoad() throws RemoteException {
try {
refresh((RegistrationPK) ctx.getPrimaryKey());
}
catch (FinderException fe) {
throw new RemoteException (fe.getMessage());
}
}
Los datos se leen desde la base de datos en un ResultSet y se asignan a las variables miembro globales para que RegistrationBean tenga la información más actualizada del usuario.
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();
}
catch (Exception ignore) {}
try {
con.close();
}
catch (Exception ignore) {}
}
}
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();
} catch (Exception ignore) {}
try {
con.close();
}
catch (Exception ignore) {}
}
}
La versión de persistencia del contenedor controlador de RegistrationBean no implementa este método porque el contenedor maneja la obtención y refresco de los datos de usuario.
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;
}