O'Reilly logo

Enterprise JavaBeans, Third Edition by Richard Monson-Haefel

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Direct Database Access from Session Beans

Perhaps the most straightforward and portable option for using a server that supports only session beans is direct database access. We did some of this with the ProcessPayment bean and the TravelAgent bean in Chapter 12. When entity beans are not an option, we simply take this option a step further. The following code is an example of the TravelAgent bean’s bookPassage() method, coded with direct JDBC data access instead of using entity beans:

public Ticket bookPassage(CreditCard card, double price) throws RemoteException, IncompleteConversationalState { if (customerID == 0 || cruiseID == 0 || cabinID == 0) { throw new IncompleteConversationalState(); } Connection con = null; PreparedStatement ps = null;; try { con = getConnection(); // Insert reservation. ps = con.prepareStatement("insert into RESERVATION "+ "(CUSTOMER_ID, CRUISE_ID, CABIN_ID, PRICE) values (?,?,?,?)"); ps.setInt(1, customerID); ps.setInt(2, cruiseID); ps.setInt(3, cabinID); ps.setDouble(4, price); if (ps.executeUpdate() != 1) { throw new RemoteException ("Failed to add Reservation to database"); } // Insert payment. ps = con.prepareStatement("insert into PAYMENT "+ "(CUSTOMER_ID, AMOUNT, TYPE, CREDIT_NUMBER, CREDIT_EXP_DATE) "+ "values(?,?,?,?,?)"); ps.setInt(1, customerID); ps.setDouble(2, price); ps.setString(3, card.type); ps.setLong(4, card.number); ps.setDate(5, new java.sql.Date(card.experation.getTime())); if (ps.executeUpdate() != 1) { throw new RemoteException ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required