Sun Java System Application Server Enterprise Edition 8.1 2005Q2 Upgrade and Migration Guide

- Previous: Migrating UIF
- Next: Migrating Rich Clients

# Migrating JDBC Code

With the JDBC API, there are two methods of database access:

- Establishing Connections Through the DriverManager Interface

  (JDBC 1.0 API), by loading a specific driver and providing a connection URL. This method is used by other Application Servers, such as IBM’s WebSphere 4.0

- Using JDBC 2.0 Data Sources

  The `DataSource` interface (JDBC 2.0 API) can be used via a configurable connection pool. According to J2EE 1.2, a data source is accessed through the JNDI naming service

Note –

Application Server does not support the Native Type 2 JDBC drivers bundled with Application Server 6.x. Code that uses the Type 2 drivers to access third party JDBC drivers, must be manually migrated.

## Establishing Connections Through the DriverManager Interface

Although this database access method is not recommended, as it is obsolete and is not very effective, there can be some applications that still use this approach.

In this case, the access code is similar to the following:

    public static final String driver = "oracle.jdbc.driver.OracleDriver";
    public static final String url = "jdbc:oracle:thin:tmb_user/tmb_user@iben:1521:tmbank";
    Class.forName(driver).newInstance();
    Properties props = new Properties();
    props.setProperty("user", "tmb_user");
    props.setProperty("password", "tmb_user");
    Connection conn = DriverManager.getConnection(url, props);

This code can be fully ported from Application Server 6.x to Application Server, as long as the Application Server is able to locate the classes needed to load the right JDBC driver. In order to make the required classes accessible to the application deployed in the Application Server, place the archive (JAR or ZIP) for the driver implementation in the `/lib` directory of the Application Server installation directory.

Modify the CLASSPATH by setting the path for the driver through the Admin Console GUI.

- Click the server instance “server1.”

- Click the tab “JVM Settings” from the right pane.

- Click the option Path Settings and add the path in the classpath suffix text entry box.

- Once the changes are made, click “Save.”

- Apply the new settings.

- Restart the server to modify the configuration file, `server.xml`.

## Using JDBC 2.0 Data Sources

Using JDBC 2.0 data sources to access a database provides performance advantages, such as transparent connection pooling, enhanced productivity by simplifying code and implementation, and code portability.

If there is a datasource by the name ”xyz’ on Application Server 6.x application and you do not want any impact on your JNDI lookup code, make sure that the datasource you create for Application Server 8.1 is prefixed with jdbc. For example: `jdbc/xyz`.

For information on configuring JDBC Datasources, see Chapter 3, JDBC Resources, in Sun Java System Application Server Enterprise Edition 8.1 2005Q2 Administration Guide.

### Looking Up the Data Source Via JNDI To Obtain a Connection

To obtain a connection from a data source, do the following:

#### To Connect to a Data Source

1. Obtain the initial JNDI context.

   To guarantee portability between different environments, the code used to retrieve an InitialContext object (in a servlet, in a JSP page, or an EJB) is as follows:

   `InitialContext ctx = new InitialContext();`

2. Use a JNDI lookup to obtain a data source reference.

   To obtain a reference to a data source bound to the JNDI context, look up the data source’s JNDI name from the initial context object. The object retrieved in this way is cast as a DataSource type object:

   `ds = (DataSource)ctx.lookup(JndiDataSourceName);`

3. Use the data source reference to obtain the connection.

   This operation requires the following line of code:

   `conn = ds.getConnection();`

   Application Server 6.x and Application Server both follow these technique to obtain a connection from the data source.

- Previous: Migrating UIF
- Next: Migrating Rich Clients

- © 2010, Oracle Corporation and/or its affiliates