Listing 1: Bean-managed Create
// ejbCreate() inserts Entity Bean
public TicketEntityPK ejbCreate
(int aTicketNum, double aPrice,
int aFlightNumber, String aSeatNumber,
Date aDepartDt, Date aArrivalDt,
String aDepartCity, String aArrivalCity,
int aPassengerNumber)
throws CreateException
{
// set attributes into Entity Bean to
// insert
ticketNum = aTicketNum;
price = aPrice;
flightNumber = aFlightNumber;
seatNumber = aSeatNumber;
departDt = aDepartDt;
arrivalDt = aArrivalDt;
departCity = aDepartCity;
arrivalCity = aArrivalCity;
passengerNumber = aPassengerNumber;
// mark as modified
isModified = true;
Connection con = null;
PreparedStatement ps =
null;
try{
con
= getConnection();
ps =
con.prepareStatement(
"insert into Ticket (ticketNum,
price, "+ "flightNumber,
seatNumber,
"+ "departDt, arrivalDt,
departCity,
"+ "arrivalCity, passengerNumber)
"+
"values (?, ?, ?, ?, ?,
?, ?, ?, ?)");
// fill values from TicketEntityBean
// into PreparedStatement
ps.setInt(1,
ticketNum);
ps.setDouble(2,
price);
ps.setInt(3,flightNumber);
ps.setString(4,seatNumber);
ps.setDate(5,
departDt);
ps.setDate(6,arrivalDt);
ps.setString(7,departCity);
ps.setString(8,
arrivalCity);
ps.setInt(9,passengerNumber);
if (ps.executeUpdate()
!= 1)
{
throw
new CreateException
("Failed
to insert ticket:
"+ticketNum);
}
TicketEntityPK
pk =
new
TicketEntityPK(ticketNum);
return pk;
}
catch (CreateException
ex){
throw
ex;
}
catch (SQLException sqlEx)
{
throw new CreateException
(sqlEx.getMessage());
}
finally {
close(ps,
con);
}
}
Listing 2: Helper methods for TicketEntityBean
// Static initializer in TicketEntityBean
// class
static
{
// initialize weblogic.jdbc.jts.Driver
new weblogic.jdbc.jts.Driver();
}
// Entity Bean helper methods
private void close
(PreparedStatement aPS, Connection aCon)
{
// final attempt to close the
// PreparedStatement and Connection
try {
aPS.close();
aCon.close();
}
catch (Exception ex) {}
}
public Connection getConnection()
throws SQLException
{
// get an open DBconnection from the
// ejbPool
return DriverManager.getConnection
("jdbc:weblogic:jts:ejbPool");
}
Listing 3: Bean-managed Read
public TicketEntityPK ejbFindByPrimaryKey
(TicketEntityPK pk)
throws FinderException, RemoteException
{
// ejbFindByPrimaryKey fetches the Entity // Bean
if ((pk == null))
throw new FinderException
("Invalid
parameter. "+
"Primary Key cannot be null");
System.out.println("ejbFindByPrima
ryKey()");
// call helper method
read(pk);
return pk;
}
private void read(TicketEntityPK pk)
throws RemoteException, FinderException {
// reads data from Ticket table
// into TicketEntityBean
Connection con = null;
PreparedStatement ps = null;
try{
con = getConnection();
ps = con.prepareStatement
("select * from Ticket
"+
"where ticketNum = ?");
ps.setInt(1, pk.ticketNum);
ps.executeQuery();
ResultSet rs = ps.getResultSet();
// map data into Entity Bean
if (rs.next()) {
ticketNum
= rs.getInt(1);
price = rs.getDouble(2);
flightNumber
= rs.getInt(3);
seatNumber
= rs.getString(4);
departDt =
rs.getDate(5);
arrivalDt
= rs.getDate(6);
departCity
= rs.getString(7);
arrivalCity
= rs.getString(8);
passengerNumber
= rs.getInt(9);
isModified
= false;
}
else {
throw new
FinderException
("Read Error: TicketEntityBean "+
pk.ticketNum + " not found");
}
}
catch (SQLException sqlEx) {
throw new RemoteException
(sqlEx.getMessage());
}
finally {
close(ps, con);
}
}
Listing 4: Bean-managed Update
public void ejbStore()
throws RemoteException
{
// saves Entity Bean to persistent storage
if (!isModified())
return;
Connection con = null;
PreparedStatement ps = null;
try {
con = getConnection();
ps = con.prepareStatement
("update Ticket set ticketNum
= ?,"+
"price = ?, flightNumber
= ?, "+
"seatNumber = ?,
departDt = ?, "+
"arrivalDt = ?,
departCity = ?, "+
"arrivalCity = ?,
passengerNumber =
?"+ "where ticketNum =
?");
// fill values from TicketEntityBean
// into PreparedStatement
ps.setInt(1, ticketNum);
ps.setDouble(2, price);
ps.setInt(3,flightNumber);
ps.setString(4,seatNumber);
ps.setDate(5, departDt);
ps.setDate(6,arrivalDt);
ps.setString(7,departCity);
ps.setString(8, arrivalCity);
ps.setInt(9,passengerNumber);
// extra for PrimaryKey where clause
ps.setInt(10, ticketNum);
int i = ps.executeUpdate();
if (i == 0) {
throw new
RemoteException
("Failed to
update TicketEntityBean:
" + ticketNum);
}
isModified = false;
}
catch (RemoteException ex)
{
throw ex;
}
catch (SQLException sqlEx)
{
throw new RemoteException
(sqlEx.getMessage());
}
finally
{
close(ps, con);
}
}
Listing 5: Bean-managed Delete
public void ejbRemove()
throws RemoteException
{
// deletes Entity Bean from
// persistent storage
Connection con = null;
PreparedStatement ps = null;
try {
con = getConnection();
TicketEntityPK pk =
(TicketEntityPK) ctx.getPrimaryKey();
ps= con.prepareStatement
("delete from Ticket where
ticketNum = ?"); ps.setInt(1, pk.ticketNum);
int i = ps.executeUpdate();
if (i == 0) {
throw new
RemoteException
("Delete failed.
TicketNumber: "
+ pk.ticketNum
+ " not found");
}
}
catch (RemoteException ex) {
throw ex;
}
catch (SQLException sqlEx) {
throw new RemoteException
(sqlEx.getMessage());
}
finally {
close(ps, con);
}
}
Listing 6: Bean-managed finder: ejbFindTickets
By Flight()
public Enumeration ejbFindTicketsByFlight
(int aFlightNumber)
throws FinderException, RemoteException
{
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = getConnection();
ps = con.prepareStatement
("select ticketNum from
Ticket "+
"where flightNumber
= ?");
ps.setInt(1, aFlightNumber);
ps.executeQuery();
rs = ps.getResultSet();
Vector ret = new Vector();
TicketEntityPK pk;
while (rs.next())
{
pk =
new TicketEntityPK(rs.getInt(1));
ret.addElement(pk);
}
rs.close();
return ret.elements();
}
catch (SQLException sqlEx) {
throw new FinderException
(sqlEx.getMessage());
}
finally {
try {
if (rs !=
null) rs.close();
if (ps !=
null) ps.close();
if (con!=
null) con.close();
}
catch (Exception done)
{}
}
}
Listing 7: Container-managed JDBC TicketEntiyBean
package jdj.ticketing.containermanaged;
import java.lang.*;
import java.rmi.*;
import java.sql.Date;
import javax.ejb.*;
/*
* Example of a container-managed Entity Bean
*/
public class TicketEntityBean extends Object
implements EntityBean
{
// TicketEntityBean business attributes
public int ticketNum;
public double price;
public int flightNumber;
public String seatNumber;
public Date departDt;
public Date arrivalDt;
public String departCity;
public String arrivalCity;
public int passengerNumber;
// tells whether or not data was
// actually modified
private boolean isModified;
// needed by Entity Bean specification
transient protected EntityContext
ctx;
// Business methods for TicketEntityBean
// Getter/Setter methods not shown in
// Listing… Methods to satifsy Entity Bean
// interface
public void ejbCreate
(int aTicketNum, double
aPrice,
int aFlightNumber, String
aSeatNumber,
Date aDepartDt, Date aArrivalDt,
String aDepartCity, String
aArrivalCity,
int aPassengerNumber)
{
// set attributes to insert
ticketNum = aTicketNum;
price = aPrice;
flightNumber = aFlightNumber;
seatNumber = aSeatNumber;
departDt = aDepartDt;
arrivalDt = aArrivalDt;
departCity = aDepartCity;
arrivalCity = aArrivalCity;
passengerNumber = aPassengerNumber;
// mark as modified
isModified = true;
}
public void ejbPostCreate
(int aTicketNum, double
aPrice,
int aFlightNumber, String
aSeatNumber,
Date aDepartDt, Date aArrivalDt,
String aDepartCity, String
aArrivalCi ty,int aPassengerNumber)
{
// do nothing
}
public void setEntityContext(EntityContext
aCtx)
{
ctx = aCtx;
}
public void unsetEntityContext()
{
ctx = null;
}
public void ejbRemove()
{
// do nothing
}
public void ejbActivate()
{
// do nothing
}
public void ejbPassivate()
{
// do nothing
}
public void ejbLoad()
{
// do nothing
}
public void ejbStore()
{
// do nothing
}
}
Listing 8: Container-Managed File TicketEntityBean
package jdj.ticketing.containermanaged.file;
import java.lang.*;
import java.rmi.*;
import java.sql.Date;
import javax.ejb.*;
/*
* Example of a container-managed
Entity Bean
*/
public class TicketEntityBean extends Object
implements EntityBean
{
// TicketEntityBean business attributes
public int ticketNum;
public double price;
public int flightNumber;
public String seatNumber;
public Date departDt;
public Date arrivalDt;
public String departCity;
public String arrivalCity;
public int passengerNumber;
// tells whether or not data was
// actually modified
private boolean isModified;
// needed by Entity Bean specification
transient protected EntityContext
ctx;
// Business methods for TicketEntityBean
// Getter/Setter methods not shown in
// Listing… Methods to satifsy Entity Bean
// interface
public void ejbCreate
(int aTicketNum, double
aPrice,
int aFlightNumber, String
aSeatNumber,
Date aDepartDt, Date aArrivalDt,
String aDepartCity, String
aArrivalCi ty, int aPassengerNumber)
{
// set attributes to insert
ticketNum = aTicketNum;
price = aPrice;
flightNumber = aFlightNumber;
seatNumber = aSeatNumber;
departDt = aDepartDt;
arrivalDt = aArrivalDt;
departCity = aDepartCity;
arrivalCity = aArrivalCity;
passengerNumber = aPassengerNumber;
// mark as modified
isModified = true;
}
public void ejbPostCreate
(int aTicketNum, double
aPrice,
int aFlightNumber, String
aSeatNumber,
Date aDepartDt, Date aArrivalDt,
String aDepartCity, String
aArrivalCity,
int aPassengerNumber)
{
// do nothing
}
public void setEntityContext(EntityCon
text aCtx)
{
ctx = aCtx;
}
public void unsetEntityContext()
{
ctx = null;
}
public void ejbRemove()
{
// do nothing
}
public void ejbActivate()
{
// do nothing
}
public void ejbPassivate()
{
// do nothing
}
public void ejbLoad()
{
// do nothing
}
public void ejbStore()
{
// do nothing
}
}