Using JDBC in a BMP Entity Bean


Now you are ready for advanced techniques using JDBC. The persistence of stored data for J2EE applications is provided through JDBC DataSource connections communicating with a relational database server. If the persistence management for an entity EJB is bean-managed, you must write JDBC code in your entity bean. The ejbCreate() method inserts the attributes of the bean as a new row in the database table. The ejbFindByPrimaryKey() method queries the database to find a row that matches the primary key. The attributes of the bean object are updated with the results returned from the SELECT operation.

The BookEntityBean is an entity EJB that is implemented using bean-managed persistence. The primary purpose for this example is to show how and where JBDC is used in an Enterprise JavaBean.

Note

Refer to Chapter 21 for further information on entity EJBs.


The BookEntityBean contains three attributes: book_id , author_id , and book_name . The ejbCreate() method for this bean inserts a new row into the books table of the database containing these three fields. A BookEntityPK object is then created using the book_id as the primary key for this entity EJB. The ejbFindByPrimaryKey() method performs a query on the database to select the row with the matching book_id . The columns from the matching row are used to initialize the corresponding attributes of the BookEntityBean object.

This example was created using Together ControlCenter version 6.0. An Entity EJB was added to the class diagram using the Entity EJB tool. The Properties Inspector includes an EJB Entity tab, which allows you to set the Persistence Management to Bean managed. The ejbCreate() and ejbFindByPrimaryKey() methods in the generated code must be modified to include your JDBC statements. The code is commented with

 
 //Write your code here 

to indicate where your code modifications should be entered. Figure 13.11 illustrates the class diagram for the BookEntityBean example, while Listing 13.8 details the Java source code for the BookEntityBean example.

Figure 13.11. The BookEntityBean class diagram.

graphics/13fig11.gif

Listing 13.8 Java Source for BookEntityBean Example
[View full width]
 /* Generated by Together */ import java.sql.SQLException; import java.sql.DriverManager; import java.sql.Connection; import java.sql.PreparedStatement; /** * @ejbHome <{BookEntityHome}> * @ejbRemote <{BookEntity}> * @ejbPrimaryKey <{BookEntityPK} * @ejbTransactionAttribute Required * @persistent*/ public class BookEntityBean implements javax.ejb.EntityBean { private javax.ejb.EntityContext ctx; public int book_id; private int author_id; private String book_name; public void setEntityContext(javax.ejb.EntityContext context) throws javax.ejb graphics/ccc.gif .EJBException { ctx = context; } public void unsetEntityContext() throws javax.ejb.EJBException { ctx = null; } public void ejbActivate() throws javax.ejb.EJBException { } public void ejbPassivate() throws javax.ejb.EJBException { } public void ejbRemove() throws javax.ejb.EJBException { } public void ejbStore() throws javax.ejb.EJBException { } public void ejbLoad() throws javax.ejb.EJBException { } public BookEntityPK ejbCreate() throws javax.ejb.CreateException, javax.ejb graphics/ccc.gif .EJBException, SQLException { // Write your code here // insert a row into the database containing this object's attributes try { Connection con = openConnection(); PreparedStatement ps = con.prepareStatement( "INSERT INTO books ( book_id, author_id, book_name ) " + "VALUES ( ?, ?, ? )" ); ps.setInteger( 1, getBook_id() ); ps.setInteger( 2, getAuthor_id() ); ps.setString ( 3, getBook_name() ); if( ps.executeUpdate() != 1 ) { throw new CreateException( "Failed to create a row in the database" ); ps.close() } BookEntityPK pk = new BookEntityPK(); pk.book_id = book_id; return pk; } catch( Exception x ) { throw new CreateException( x.getMessage() ) } return null; } public void ejbPostCreate() throws javax.ejb.CreateException, javax.ejb.EJBException, graphics/ccc.gif SQLException { // Write your code here } public int getBook_id(){ return book_id; } public void setBook_id(int param){ this.book_id = param; } public BookEntityPK ejbFindByPrimaryKey(BookEntityPK pk) throws javax.ejb graphics/ccc.gif .FinderException, javax.ejb.EJBException { // Write your code here // refresh this object's attributes by searching the database // for the primary key if( pk == null ) { throw new FinderException( "primary key cannot be null" ); } try { Connection con = openConnection(); PreparedStatement ps = con.prepareStatement( "SELECT author_id, book_name FROM books " + "WHERE book_id = ?" ); ps.setInteger( 1, pk.book_id ); ps.executeQuery(); ResultSet rs = ps.getResultSet(); if( rs. next () ) { book_id = pk.book_id; author_id = rs.getInt( 1 ); book_name = rs.getString( 2 ); } else { throw new FinderException( "Could not find " + pk.book_id ); } ps.close(); } catch( Exception x ) { throw new FinderException( x.getMessage() ); } return pk } public int getAuthor_id(){ return author_id; } public void setAuthor_id(int property){ this.author_id = property; } public String getBook_name(){ return book_name; } public void setBook_name(String property){ this.book_name = property; } // create a static instance of the database driver static { new Weblogic.jdbc.jts.Driver(); } // get Connection from DriverManager public Connection openConnection() throws SQLException { return DriverManager.getConnection( "jdbc:Weblogic:jts:ejbPool" ); } } 


BEA WebLogic Platform 7
BEA WebLogic Platform 7
ISBN: 0789727129
EAN: 2147483647
Year: 2003
Pages: 360

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net