Java Reference
In-Depth Information
if (ps != null) {
ps.close();
}
} catch (Exception e) {
System.out.println("Error closing PreparedStatement: "+e);
throw new EJBException (e);
}
try {
if (con != null) {
con.close();
}
} catch (Exception e) {
System.out.println("Error closing Connection: " + e);
throw new EJBException (e);
}
}
}
The implementation class uses all the methods defined in the home and remote interfaces, as well as
the methods defined in the EntityBean interface. Table 21-2 summarizes the database-access calls in
the MemberBean class.
Table 21-2: Database -access Operations in MemberBean
Method
SQL Statement
Functionality
ejbCreate
INSERT
Create entity object in database and
initialize instance variables
ejbRemove
DELETE
Delete entity object from database and
disacssociate bean instance
ejbFindByPrimaryKey
SELECT
Locate entity object with given primary key
and associate bean instance with the
object
ejbFindByMembershipYear
SELECT
Locate all entity objects that have been
members longer than a given number of
years
ejbLoad
SELECT
Refresh instance variables with current
object state stored in database
ejbStore
UPDATE
Update state stored in database with
current instance variable values
The business methods of the MemberBean class (such as getLastname() , setLastName(String) ,
and so on) are absent from this table because they do not access the database. Instead, these
business methods update the instance variables, which are written to the database when the EJB
container calls ejbStore . After the EJB container calls the ejbLoad method, the instance variables
are refreshed with the current entity-object state, and the getter function retrieves the refreshed
variables. Another developer might have chosen to access the database in the business methods of the
MemberBean class. This choice is one of those design decisions that depend on the specific needs of
your application. For the simple entity bean like the example MemberEJB , database access from
business methods is not warranted. But in some cases, the business logic may be complex, and there
may be a need to access the database from the business method.
Search WWH ::




Custom Search