package testbmp2;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class CabinBean implements EntityBean {
public EntityContext entityContext;
public Integer ID;
public
String Name;
//next is ejbCreate **********************************************************
public Integer ejbCreate(java.lang.Integer id) throws CreateException {
this.ID = id;
Connection conn = null;
PreparedStatement ps = null;
try
{
try
{
conn = getConnection();
}
catch(Exception e)
{
e.printStackTrace();
}
ps = conn.prepareStatement("insert into Cabin (id, name) values (?, ?)");
ps.setInt(1, id.intValue());
ps.setString(2, "");
if(ps.executeUpdate() != 1)
{
throw new CreateException("Filed to add Cabin to database!");
}
return id;
}
catch(SQLException e)
{
throw new EJBException(e);
}
finally
{
try
{
if(ps != null)
{
ps.close();
}
if(conn != null)
{
conn.close();
}
}
catch(SQLException e)
{
e.printStackTrace();
}
}
}
........................
package testbmp2;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface CabinRemote extends javax.ejb.EJBObject {
public void setId(java.lang.Integer id) throws RemoteException;
public java.lang.Integer getId() throws RemoteException;
public void setName(java.lang.String name) throws RemoteException;
public java.lang.String getName() throws RemoteException;
}
..................................
package testbmp2;
import javax.ejb.*;
import java.util.*;
import java.rmi.*;
public interface CabinRemoteHome extends javax.ejb.EJBHome {
public CabinRemote create(java.lang.Integer id) throws CreateException, RemoteException;
public CabinRemote findByPrimaryKey(java.lang.Integer id) throws FinderException,RemoteException;
}
....................................
package testbmp2;
import javax.naming.*;
import java.util.Properties;
import javax.rmi.PortableRemoteObject;
public class CabinTestClient1 {
private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. It must be created by calling one of the Home interface methods first.";
private static final int MAX_OUTPUT_LINE_LENGTH = 100;
private boolean logging = true;
private CabinRemoteHome cabinRemoteHome = null;
private CabinRemote cabinRemote = null;
//CabinTestClient1 cabinTestClient1 = new CabinTestClient1();
//Construct the EJB
test client
public CabinTestClient1() {
long startTime = 0;
if (logging) {
log("Initializing bean access.");
startTime = System.currentTimeMillis();
}
try {
//get naming context
Context ctx = getInitialContext();
//look up jndi name
Object ref = ctx.lookup("CabinRemote");
//cast to Home interface
cabinRemoteHome = (CabinRemoteHome) PortableRemoteObject.narrow(ref, CabinRemoteHome.class);
if (logging) {
long endTime = System.currentTimeMillis();
log("Succeeded initializing bean access.");
log("Execution time: " + (endTime - startTime) + " ms.");
}
try
{
java.lang.Integer id = new Integer("112");
log("The id ="+id);
//cabinRemote = cabinRemoteHome.findByPrimaryKey(id);
CabinRemote cabinRemote = cabinRemoteHome.create(id);
cabinRemote.setName("runFrank");
}
catch(Exception e)
{
log("@The error is:"+e);
e.printStackTrace();
}
.........