Database Connections

The Application Server ships with a relational database product named Derby.

Note: Application Server 8.2 includes a copy of the open source Derby database server. Application Server 8.0/ 8.1 includes the PointBase database server. If you are using Application Server 8.0/8.1, either follow the instructions in the J2EE Tutorial at that works with Application Server 8.0/8.1 or upgrade to Application Server 8.2 (see to download).

The following material shows how the SavingsAccountBean example of Chapter 26 accesses a Derby database. The SavingsAccountBean component is an entity bean with bean-managed persistence.

Session beans and web components will use the same approach as SavingsAccountBean to access a database. (Entity beans with container-managed persistence are different. See Chapter 27.)

Coding a Database Connection

For the SavingsAccountBean example, the code that connects to the database is in the entity bean implementation class SavingsAccountBean. The source code for this class is in this directory:


The bean connects to the database in three steps:

  1. Specify the logical name of the database.
  2.   private String dbName
        = "java:comp/env/jdbc/SavingsAccountDB";

    The java:comp/env portion of the logical name is the environment naming context of the component. The jdbc/SavingsAccountDB string is the resource reference name (sometimes referred to as the coded name). In deploytool, you specify the resource reference name and then map it to the JNDI name of the DataSource object.

  3. Obtain the DataSource object associated with the logical name.
  4.   InitialContext ic = new InitialContext();
      DataSource ds = (DataSource) ic.lookup(dbName);

    Given the logical name for the resource, the lookup method returns the DataSource object that is bound to the JNDI name in the directory.

  5. Get the Connection object from the DataSource object.
  6.   Connection con = ds.getConnection();

Specifying a Resource Reference

The application for the SavingAccountBean example is in the SavingsAccountApp.ear file, which is in this directory:


For your convenience, the resource reference and JNDI names in SavingsAccountApp.ear have already been configured in deploytool. However, you may find it instructive to open SavingsAccountApp.ear in deploytool and follow these steps for specifying the resource reference.

  1. In deploytool, select SavingsAccountBean from the tree.
  2. Select the Resource Ref's tab.
  3. Click Add.
  4. In the Coded Name field, enter jdbc/SavingsAccountDB.
  5. In the Type combo box, select javax.sql.DataSource.
  6. In the Authentication combo box, select Container.
  7. If you want other enterprise beans to share the connections acquired from the DataSource, select the Sharable checkbox.
  8. To map the resource reference to the data source, enter jdbc/ejbTutorialDB in the JNDI Name field.

If the preceding steps are followed, the Resource Ref's tab will appear as shown in Figure 31-1.

Resource Refs Tabbed Pane of SavingsAccountEJB

Figure 31-1 Resource Ref's Tabbed Pane of SavingsAccountBean

Creating a Data Source

In the preceding section, you map the resource reference to the JNDI name of the data source. The deploytool utility stores this mapping information in a deployment descriptor of SavingsAccountBean. In addition to setting the bean's deployment descriptor, you also must define the data source in the Application Server. You define a data source by using the Admin Console. To create the data source with the Admin Console, follow this procedure:

  1. Open the URL http://localhost:4848/asadmin in a browser.
  2. Expand the JDBC node.
  3. Select the JDBC Resources node.
  4. Click New.
  5. Type jdbc/ejbTutorialDB in the JNDI Name field.
  6. Choose DerbyPool from the Pool Name combo box.
  7. Click OK.
  8. Note that jdbc/ejbTutorialDB is listed under the JDBC Resources node.