Databases Reference
In-Depth Information
5.8 Introduction to javax.sql package
The javax.sql package defines extension to the JDBC 2.0 API to support
connection pooling, data source, distributed transaction, and so on. DB2
universal database implements all of these interfaces. DataSource
implementation is explained below.
5.8.1 DataSource
Using DriverManager to get the database connection makes the application
dependent on the vendor specific URL and driver classes. A DataSource object
can be used to get the connection, which is portable among the different data
sources, hence, not dependent on the specific URL format and driver classes.
This portability is gained by registering the different data sources with the JNDI
service (javax.naming service) and getting the information about any data
sources by providing the logical name of the datasource in the application. A
DataSource object is either created in the application itself or by a lookup in the
JNDI service. Before making use of lookup in JNDI services, you need to register
the database to JNDI.
The Example 5-17 gives an example of creating DataSource object by JNDI
lookup service and getting the connection to the data source.
Example 5-17 Creating DataSource by JNDI lookup service
Context ctx=new InitialContext();
DataSource ds=(DataSource)ctx.lookup("sampledb");
Connection con=ds.getConnection();
A DataSource object can be bound to a logical name by registering it to JNDI.
Registering to JNDI services requires setting all of the properties to get a
connection such as database name, user name, password, and so on. Once the
data source is registered, an application needs to know only the logical name to
retrieve the database connection. DB2 universal driver provides the following
implementation for the DataSource:
com.ibm.db2.jcc.DB2SimpleDataSource
A data source can be registered using this implementation as shown in
Example 5-18.
Search WWH ::




Custom Search