DB2 Version 9.7 for Linux, UNIX, and Windows

Connecting to a data source using the DataSource interface

If your applications need to be portable among data sources, you should use the DataSource interface.

Using DriverManager to connect to a data source reduces portability because the application must identify a specific JDBC driver class name and driver URL. The driver class name and driver URL are specific to a JDBC vendor, driver implementation, and data source.

When you connect to a data source using the DataSource interface, you use a DataSource object.

The simplest way to use a DataSource object is to create and use the object in the same application, as you do with the DriverManager interface. However, this method does not provide portability.

The best way to use a DataSource object is for your system administrator to create and manage it separately, using WebSphere® Application Server or some other tool. The program that creates and manages a DataSource object also uses the Java™ Naming and Directory Interface (JNDI) to assign a logical name to the DataSource object. The JDBC application that uses the DataSource object can then refer to the object by its logical name, and does not need any information about the underlying data source. In addition, your system administrator can modify the data source attributes, and you do not need to change your application program.

To learn more about using WebSphere to deploy DataSource objects, go to this URL on the web:
http://www.ibm.com/software/webservers/appserv/

To learn about deploying DataSource objects yourself, see "Creating and deploying DataSource objects".

You can use the DataSource interface and the DriverManager interface in the same application, but for maximum portability, it is recommended that you use only the DataSource interface to obtain connections.

To obtain a connection using a DataSource object that the system administrator has already created and assigned a logical name to, follow these steps:

  1. From your system administrator, obtain the logical name of the data source to which you need to connect.
  2. Create a Context object to use in the next step. The Context interface is part of the Java Naming and Directory Interface (JNDI), not JDBC.
  3. In your application program, use JNDI to get the DataSource object that is associated with the logical data source name.
  4. Use the DataSource.getConnection method to obtain the connection.
    You can use one of the following forms of the getConnection method:
    getConnection();
    getConnection(String user, String password);
    Use the second form if you need to specify a user ID and password for the connection that are different from the ones that were specified when the DataSource was deployed.

Examples

Example of obtaining a connection using a DataSource object that was created by the system administrator: In this example, the logical name of the data source that you need to connect to is jdbc/sampledb. The numbers to the right of selected statements correspond to the previously-described steps.

Figure 1. Obtaining a connection using a DataSource object
import java.sql.*;
import javax.naming.*;
import javax.sql.*;
…
Context ctx=new InitialContext();                       2 
DataSource ds=(DataSource)ctx.lookup("jdbc/sampledb");  3 
Connection con=ds.getConnection();                      4 
Example of creating and using a DataSource object in the same application:
Figure 2. Creating and using a DataSource object in the same application
import java.sql.*;        // JDBC base
import javax.sql.*;       // Additional methods for JDBC
import com.ibm.db2.jcc.*; // IBM Data Server Driver for JDBC and SQLJ   1 
                          // interfaces
DB2SimpleDataSource dbds=new DB2SimpleDataSource();             2 
dbds.setDatabaseName("dbloc1");                                 3 
                          // Assign the location name
dbds.setDescription("Our Sample Database");
                          // Description for documentation
dbds.setUser("john");
                          // Assign the user ID
dbds.setPassword("dbadm");
                          // Assign the password
Connection con=dbds.getConnection();                            4 
                          // Create a Connection object
Note Description
1 Import the package that contains the implementation of the DataSource interface.
2 Creates a DB2SimpleDataSource object. DB2SimpleDataSource is one of the IBM® Data Server Driver for JDBC and SQLJ implementations of the DataSource interface. See "Creating and deploying DataSource objects" for information on DB2's DataSource implementations.
3 The setDatabaseName, setDescription, setUser, and setPassword methods assign attributes to the DB2SimpleDataSource object. See "Properties for the IBM Data Server Driver for JDBC and SQLJ" for information about the attributes that you can set for a DB2SimpleDataSource object under the IBM Data Server Driver for JDBC and SQLJ.
4 Establishes a connection to the data source that DB2SimpleDataSource object dbds represents.