InfoCenter Home >
4: Developing applications >
4.2: Building Web applications >
4.2.4: Accessing data >
4.2.4.2: Obtaining and using database connections >
4.2.4.2.2: Accessing data with the JDBC 2.0 Core API

4.2.4.2.2: Accessing data with the JDBC 2.0 Core API

WebSphere applications can access a relational database directly through a JDBC provider that uses the JDBC 2.0 Core API. You access a relational database in this manner as follows:

  1. Establish a connection through the DriverManager class
  2. Send SQL queries or updates to the database management system
  3. Process the results

Only a single connection is obtained. This connection does not belong to a pool of connections and is not managed by IBM WebSphere Application Server. It is the responsibility of the application to manage the use of this connection.

The following code fragment shows a simple example of obtaining and using a connection directly through a JDBC provider:

try {
// establish a connection through the DriverManager
Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
String url = "jdbc:db2:sample";
String username = "dbuser";
String password = "passwd";
java.sql.Connection conn =
java.sql.DriverManager.getConnection(url, username, password);

// query the database
java.sql.Statement stmt = conn.createStatement();
java.sql.ResultSet rs =
stmt.executeQuery("SELECT EMPNO, FIRSTNME, LASTNAME FROM SAMPLE");

// process the results
while (rs.next()) {
String empno = rs.getString("EMPNO");
String firstnme = rs.getString("FIRSTNME");
String lastname = rs.getString("LASTNAME");
// work with results
}
}
catch (java.sql.SQLException sqle) {
// handle the SQLException
}
finally {
try {
if(rs != null) rs.close();
}
catch (SQLException sqle) {
// can ignore
}
try {
if(stmt != null) stmt.close();
}
catch (SQLException sqle) {
// can ignore
}
try {
if(conn != null) conn.close();
}
catch (SQLException sqle) {
// can ignore
}
}

In the previous example, the first action is to establish a connection to the database. This is done by loading and registering the JDBC provider and then requesting a connection from DriverManager. DriverManager, a JDBC 1.0 class, is the basic service for managing a set of JDBC providers. It is necessary to load the driver class before the call to getConnection(), because DriverManager can establish a connection only to a driver that has registered with it. Loading the driver class also registers it with DriverManager.

After a connection has been obtained, the database is queried by creating a statement and executing a query on that statement. The query results are put into a ResultSet object.

Lastly, the results are processed by stepping through the result set and pulling the data from each record retrieved.

According to the JDBC 2.0 Core API specification, the DriverManager class has been deprecated. Therefore, any application using this class should be rewritten to use WebSphere connection pooling, which uses the datasource method described in the JDBC 2.0 Optional Package API to obtain connections to the database. For more information, see the JDBC 2.0 Core API specification.

Go to previous article: Handling data access exceptions Go to next article: Accessing relational databases with the IBM data access beans

 

 
Go to previous article: Handling data access exceptions Go to next article: Accessing relational databases with the IBM data access beans