Introduction to javax.sql package - IBM DB2

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.

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 gives an example of creating DataSource object by JNDI lookup service and getting the connection to the data source.

Creating DataSource by JNDI lookup service

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 below

Registering data source

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

IBM DB2 Topics