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.
About this task
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.
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.
Procedure
To obtain a connection using a DataSource object that the system administrator has already created and assigned a logical name to, follow these steps:
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.
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
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 the DataSource implementations of Db2®. |
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. |