Tip

DB2 Java connectivity with JDBC

Kulvir Bhogal and Grant Hutchinson

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) 
    {
       try
       {
           // load the DB2 Driver
           Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
           // establish a connection to DB2
           Connection db2Conn = 
         DriverManager.getConnection
             ("jdbc:db2:phoneDB","db2admin","db2admin");
            // 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);
               System.out.println("-------------------------------");
           }
           // clean up resources
           resultSet.close();
           st.close();
           db2Conn.close();
       }
       catch (ClassNotFoundException cnfe)
       {
           cnfe.printStackTrace();
       }
       catch (SQLException sqle)
       {
           sqle.printStackTrace();
       }
    }
}

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.