Moving to Enterprise JavaBeans 187
17.3.1 Adding Behavior to Your EJB
When you create an EJB in the EJB development environment, a set of
required methods for the bean is automatically created. You can modify these
methods and add new remote (business) and home methods of your own.
Adding methods in the EJB development environment is similar to adding
methods elsewhere in VisualAge for Java. Once you have created remote
(business) methods or home methods for your classes and interfaces, you
can promote the methods to the remote and home interfaces from the Bean
class without editing the remote and home interfaces directly.
Follow these steps to add a new method:
1. In the Types pane of the EJBs page, select the class or interface to which
you want to add a method.
2. Click mouse button 2, then select Add Method from the pop-up menu.
The Add Method SmartGuide appears.
3. Follow the SmartGuide instructions on creating a new method.
4. Click Finish to create and compile the new method.
If you want to remove a remote method from the remote interface, select the
method and then select Methods-> Remove From ->EJB Remote Interface.
If you want to remove a home method from the home interface, select the
method and then select Methods-> Remove From ->EJB Home Interface.
Before we add methods to our EJB, we are going to define some properties
that are used later on. We add the following properties to the
EmployeeIDBean class:
private Connection _connection;
private static String _jdbcURL = "jdbc:db2:sample";
private static String _jdbcDriverName = "COM.ibm.db2.jdbc.app.DB2Driver";
public String userid;
private Statement _statement;
and an import statement:
import java.sql.*;
In our EmployeeID bean, we are going to add two methods:
getConnection()
This method loads the JDBC driver and returns a connection
188 Using VisualAge for Java Enterprise Version 2 to Develop CORBA and EJB Applications
private Connection getConnection()
{
System.out.println("getConnection ");
if (_connection == null)
{
try
{
Class.forName(_jdbcDriverName).newInstance();
_connection = DriverManager.getConnection(_jdbcURL);
}
catch(Exception e)
{
System.out.println("getConnection error ");
e.printStackTrace();
}
}
return _connection;
}
authenticate(String)
This method compares the user entered password against the first name
found in the database table corresponding to the serial number. If they
match, the user has been authenticated.
public boolean authenticate(String password) throws
java.rmi.RemoteException
{
return password.equals(userFirstname);
}
If you want to promote a remote method to the remote interface, select the
method and then select Methods->Add to->EJB Remote Interface.
If you want to promote a home method to the home interface, select the
method and then select Methods->Add to->EJB Home Interface.
Typically, to add a method in a development environment that uses interfaces
and implementations, you would add the method first to the interface, and
then to the implementation class. However, the EJB development model is
slightly different. If you browse the EmployeeID bean, you will notice that it
does not implement the EmployeeID interface. The EmployeeID interface is a
generated class, and if you add method signatures to it, they will be
overwritten.
Moving to Enterprise JavaBeans 189
So, add the method to the interface, using the Add To->EJB Remote
Interface menu items (see Figure 82).
Figure 82. Adding Method to EJB Interface
The EmployeeID interface now contains an authenticate() method.
Now we need to modify some of the generated methods to add additional
behavior:
1. ejbActivate()
We just request the connection by calling the added method
getConnection.
public void ejbActivate() throws java.rmi.RemoteException {
getConnection();
}
2. ejbCreate(EmployeeIDKey)
public /**@EJBHome*/ EmployeeIDKey ejbCreate(EmployeeIDKey key) throws
javax.ejb.CreateException {
190 Using VisualAge for Java Enterprise Version 2 to Develop CORBA and EJB Applications
primaryKey=key.primaryKey;
return key;
}
3. ejbFindByPrimaryKey(EmployeeIDKey)
public /**@EJBHome*/ EmployeeIDKey ejbFindByPrimaryKey (EmployeeIDKey key) throws
javax.ejb.FinderException, RemoteException {
Statement statement = null;
ResultSet rs = null;
System.out.println("ejbFindByPrimaryKey ");
try
{
ejbActivate();
String sql = "SELECT FIRSTNME FROM EMPLOYEE WHERE EMPNO = ’"+key.primaryKey+"’";
statement = getConnection().createStatement();
rs = statement.executeQuery(sql);
if (rs.next()){
//System.out.println("firstname is:" + rs.getString("FIRSTNME").trim());
userFirstname = rs.getString("FIRSTNME").trim();
System.out.println("firstname is:" + userFirstname);
return new EmployeeIDKey(key.primaryKey);
}
else
throw new FinderException("Employee ID not found: "+key.primaryKey);
}
catch(SQLException e)
{
throw new RemoteException(e.getMessage(), e);
}
finally
{
try
{
statement.close();
}
catch(Exception e){
}
}
}
4. ejbPassivate
public void ejbPassivate() throws java.rmi.RemoteException {
System.out.println("ejbPassivate ");
try
{
getConnection().close();
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
_connection = null;
}
}

Get Using VisualAge for Java Enterprise Version 2 to Develop CORBA and EJB Applications now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.