DB2 Java connectivity with JDBC

One way to use Java in DB2 UDB v8 is via JDBC drivers. Here's some sample code showing how to connect to a DB via JDBC.

Courtesy of InformIT.

DB2 UDB provides for JDBC connectivity through a number of Java classes and interfaces that implement the JDBC specification. The implementation is known as a JDBC driver. DB2 currently offers JDBC drivers known as legacy/CLI drivers and the universal JDBC driver.

The legacy/CLI JDBC driver is provided in the file db2java.zip which is in the default DB2 installation directory. For Java development, the only required DB2 software is the runtime client, and it is a much smaller download image (around 68MB for Windows and 200MB for Linux). If you decide that the Universal JDBC driver in type 4 mode is all you really need, you can simply copy the db2jcc.jar file and its corresponding license jar file from an existing installation. These two files would result in less than 1MB of disk space.

The code below shows how to programmatically access the database "phonedb." Note that the implementation package name is com.ibm.db2.jdbc.app.DB2Driver. This example simply retrieves data from the database phonedb using a simple SQL query.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Type2ExampleLegacy {

    public static void main(String[] args) 
           // load the DB2 Driver
           // establish a connection to DB2
           Connection db2Conn = 
            // use a statement to gather data from the database
            Statement st = db2Conn.createStatement();
            String myQuery = "SELECT * FROM PHONENUMBERS"; 
           // execute the query
            ResultSet resultSet = st.executeQuery(myQuery); 
            // cycle through the resulSet and display what was grabbed
            while (resultSet.next())
             String name = resultSet.getString("name");
             String phoneNumber = resultSet.getString("phonenumber");
               System.out.println("Name: " + name);
               System.out.println("Phone Number: " + phoneNumber);
           // clean up resources
       catch (ClassNotFoundException cnfe)
       catch (SQLException sqle)

With the advent of DB2 8.1 came the new Universal JDBC driver, which can be used either in a Type 2 architecture or a Type 4 architecture. In a Type 2 mode, the Universal JDBC driver provides local application performance gains (because it avoids using TCP/IP protocol to communicate to the DB2 server). In Type 2 mode, there is also full support for distributed transactions. Therefore, if the application and the DB2 server are on the same machine, we recommend that you use the Universal JDBC driver in Type 2 mode. The driver is located in the file db2jcc.jar. Note that at runtime, in type 2 mode an attempt will be made to load a file named db2jcct2.dll. This file is installed when the Runtime Client or Application Development client of the product are installed.

Read about the differences between Type 2 and Type 4 drivers and see further code examples at InformIT.

This was first published in June 2004

Dig Deeper on DB2 UDB (universal databases)



Forgot Password?


Your password has been sent to: