Access data from J2EE application clients

The J2EE application client does not support the look up or direct access of data source resources that have been configured on WAS, because the J2EE application client does not support Java 2 Connection Factories. To use a data source directly from the client application, configure your data source using the Application Client Resource Configuration Tool (ACRCT).

In addition, WAS and WAS clients do not provide client database drivers to use directly from a J2EE application client. If your application client directly uses a database, provide the database drivers on the client. This can involve contacting your database vendor to acquire client database driver code and licenses.

Instead of directly accessing the database, it is recommended that you use an enterprise bean in your client application. Accessing a database through an enterprise bean eliminates the need to have database drivers on the client machine, because the database access is handled by the enterprise bean that runs on WAS.

To access a database directly from a J2EE application client, you retrieve a javax.sql.DataSource object from a resource reference configured in the client deployment descriptor. This resource reference is configured as part of the deployment descriptor for the client application and provides a reference to a preconfigured data source object.

Data access from an application client uses the JDBC driver connection functionalities directly from the client side. It does not take advantage of the additional pooling support available in the application server run time. For this reason, your client application should use an enterprise bean running on the server side to perform data access. This enterprise bean can take advantage of the connection reuse and added functionality provided by the product run time.

  1. Import the appropriate JDBC API and naming packages:
    import java.sql.*;
    import javax.sql.*;
    import javax.naming.*;
  2. Create the initial naming context:
    InitialContext ctx = new InitialContext();
  3. Use the InitialContext to look up a data source object from a resource reference.
    javax.sql.DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/myDS");

    where jdbc/myDS is the name of the resource reference.

  4. Get a java.sql.Connection from the data source.

    • If no user ID and password are required for the connection, or if you plan to use the defaultUser and defaultPassword that are specified when the data source is created in the Application Client Resource Configuration tool (ACRCT) in a future step:
      java.sql.Connection conn = ds.getConnection();
    • Otherwise, make the connection with a specific user ID and password:
      java.sql.Connection conn = ds.getConnection("user", "password");

      where user and password are the user id and password for the connection

  5. Run a database query using the java.sql.Statement, java.sql.PreparedStatement, or java.sql.CallableStatement interfaces as appropriate.
    Statement stmt = conn.createStatement();
    String query   = "Select FirstNme from " + owner.toUpperCase() + ".Employee 
    where LASTNAME = '" + searchName + "'";
    ResultSet rs   = stmt.executeQuery(query);
    while (rs.next()) {    firstNameList.addElement(rs.getString(1));
    }
  6. Close the database objects used in the previous step, including any ResultSet, Statement, PreparedStatement, or CallableStatement objects.

  7. Close the connection. Ideally, close the connection in a finally block of the try...catch wrapped around the database operation. This action ensures that the connection gets closed, even in the case of an exception.
    conn.close();

External Java clients cannot use component managed authentication data that is defined for a resource. As a result, if your application client needs to access a data source that uses component managed authentication, the client must provide authentication data. For more information, see Lookup security with component managed authentication.

See Supported hardware, software, and APIs Link outside Information Center for a current list of providers that are supported on WAS.