Sun Java System Application Server Platform Edition 8.1 2005Q2 Update 2 Upgrade and Migration Guide

- Previous: Establishing Connections Through the DriverManager Interface
- Next: Migrating Rich Clients

## To Connect to a Data Source

##### Steps

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: Establishing Connections Through the DriverManager Interface
- Next: Migrating Rich Clients

- © 2010, Oracle Corporation and/or its affiliates