User Tools

Site Tools


software:db2:db2_jdbc_drivers

This is an old revision of the document!


DB2 JDBC drivers

There are 2 types of jdbc drivers supported

  • The legacy based CLI driver(Type 2)
  • The new JDBC Universal driver(Type 4)

History behind the jdbc drivers

In order to understand how we came to the development of the DB2 Universal Driver, you need to understand how the JDBC specification defines the different types of drivers in the Java world.

Type 1 driver This type of driver code maps directly to a high level native API. JDBC and ODBC are similar APIs, so this type of driver is usually associated with the JDBC-ODBC bridge. This driver does not have too much context with respect to the DB2 UDB product.

Type 2 driver

A T2 driver has a native component that is part of the driver, but separate from the data access API. The native component and the Java component make up this driver. For DB2 UDB, the DB2 CLI libraries comprise the native component.

Type 3 driver

This is a Java client that communicates using a database independent protocol. Since the protocol is database independent, the advantage of this protocol falls to middleware servers that act as gateways to heterogeneous backend servers.

Type 4 driver

  This driver is pure Java and implements the network protocol for a specific data
  source. The client connects directly to the data source.

DB2 JDBC support is provided as part of the Java enablement option for DB2 UDB clients and servers. You just need to make sure that you have the appropriate Java developer kit downloaded

Here is a comparison table

CLI legacy driver Universal driver
db2java.zip db2jcc.jar
IN Unix: Use Type2 by placingjava/db2jcc.jar and
java/db2java.zip in CLASSPATHdb2jcc_license_cu.jar in CLASSPATH
Connection comparison
DB2 uses CLI interface network communication. DB2 UDB uses
CLI is the native component distributed relational database architecture
that communicates with the (DRDA) to communicate to the server
database server.This legacy based This is a pure Java driver,run independently
CLI driver requires common client of what product is installed on the machine
code
Driver Initialization
3 steps necessary to load this Universal supports type 2 and Type4
driver. 1. Import java.sql* corefrom a single driver.h network communication
2. Load the JDBC driver Class.forNameuses DRDA to communicate to server and
(COM.ibm.db2.jdbc.app.DB2Driver).and flow requests to the database server
3.Specify the connection URL: The following means that a Type 2 driver is
DriverManager getConnection being used:jdbc:db2:database
jdbc:db2:sample Type4 : jdbc:db2server:port/database | | |jdbc:db2server/database
Error Handling
The legacy driver gets its errorThe universal driver does not attempt to
messages from the DB2 product andrecreate pre-existing SQL error codes
essentially spits out the entire has its own defined error codes
error message back to the applicationin the range +/-4200 and +/-4299
IBM Data Server Driver for JDBC and SQLJ (type 2 and type 4)

The IBM Data Server Driver for JDBC and SQLJ is a single driver that includes JDBC type 2 and JDBC type 4 behavior. When an application loads the IBM Data Server Driver for JDBC and SQLJ, a single driver instance is loaded for type 2 and type 4 implementations. The application can make type 2 and type 4 connections using this single driver instance. The type 2 and type 4 connections can be made concurrently. IBM Data Server Driver for JDBC and SQLJ type 2 driver behavior is referred to as IBM Data Server Driver for JDBC and SQLJ type 2 connectivity. IBM Data Server Driver for JDBC and SQLJ type 4 driver behavior is referred to as IBM Data Server Driver for JDBC and SQLJ type 4 connectivity.

Two versions of the IBM Data Server Driver for JDBC and SQLJ are available. IBM Data Server Driver for JDBC and SQLJ version 3.5x is JDBC 3.0-compliant. IBM Data Server Driver for JDBC and SQLJ version 4.x is compliant with JDBC 4.0 or later.
The IBM Data Server Driver for JDBC and SQLJ is the default driver for Java routines.

The DB2 JDBC Type 2 Driver for Linux, UNIX and Windows will not be supported in future releases. You should therefore consider moving to the IBM Data Server Driver for JDBC and SQLJ.

Compatibility for IBM Data Server Driver for JDBC and SQLJ type 4 connectivity

The IBM Data Server Driver for JDBC and SQLJ is always downward compatible with DB2
databases at the previous release level. For example, IBM Data Server Driver for JDBC and SQLJ type 4 connectivity from the IBM Data Server Driver for JDBC and SQLJ version 3.61, which is shipped with DB2 Database for Linux, UNIX, and Windows Version 9.7 Fix Pack 3, to a DB2 Database for Linux, UNIX, and Windows Version 8 database is supported.


Compatibility for IBM Data Server Driver for JDBC and SQLJ type 2 connectivity

In general, IBM Data Server Driver for JDBC and SQLJ type 2 connectivity is intended for connections to the local database system, using the driver version that is shipped with that database version. For example, version 3.6x of the IBM Data Server Driver for JDBC and SQLJ is shipped with DB2 Database for Linux, UNIX, and Windows Version 9.5 and Version 9.7,

However, for IBM Data Server Driver for JDBC and SQLJ type 2 connectivity to a local DB2 Database for Linux, UNIX, and Windows database, the database version can be one version earlier or one version later than the DB2 Database for Linux, UNIX, and Windows version with which the driver was shipped. For IBM Data Server Driver for JDBC and SQLJ type 2 connectivity to a local DB2 for z/OS subsystem, the subsystem version can be one version later than the DB2 for z/OS version with which the driver was shipped.

For our Community version:
DB2 Version 10.1 Fix Pack 1 	3.64.xx, 4.14.xx
DB2 Version 10.1 	3.63.xx, 4.13.xx


How DB2 applications connect to a data source using the DriverManager interface with the DB2 JDBC Type 2 Driver

A JDBC application can establish a connection to a data source using the JDBC DriverManager interface, which is part of the java.sql package.
JDBC Type 2 Driver for Linux
you load the driver by invoking the Class.forName method with the following argument:

COM.ibm.db2.jdbc.app.DB2Driver

The following code demonstrates loading the DB2 JDBC Type 2 Driver:

try {
  // Load the DB2 JDBC Type 2 Driver with DriverManager
  Class.forName("COM.ibm.db2.jdbc.app.DB2Driver");
} catch (ClassNotFoundException e) {
     e.printStackTrace();
}

The catch block is used to print an error if the driver is not found.
After you load the driver, you connect to the data source by invoking the DriverManager.getConnection method. You can use one of the following forms of getConnection:

getConnection(String url);
getConnection(String url, user, password);
getConnection(String url, java.util.Properties info); 

The url argument represents a data source.

For the DB2 JDBC Type 2 Driver, specify a URL of the following form:

jdbc:db2:
    jdbc:db2: indicates that the connection is to a DB2 data source
database
    A database alias. The alias refers to the DB2 database catalog entry on the DB2 client.
The info argument is an object of type java.util.Properties that contains a set of driver properties for the connection. 


The info argument is an object of type java.util.Properties that contains a set of driver properties for the connection. 


eg:
Example: Setting the user ID and password in user and password parameters:

String url = "jdbc:db2:toronto";
                                          // Set URL for data source
String user = "db2adm";
String password = "db2adm";
Connection con = DriverManager.getConnection(url, user, password); 
                                          // Create connection

Example: Setting the user ID and password in a java.util.Properties object:

Properties properties = new Properties(); // Create Properties object
properties.put("user", "db2adm");         // Set user ID for connection
properties.put("password", "db2adm");     // Set password for connection
String url = "jdbc:db2:toronto";
                                          // Set URL for data source
Connection con = DriverManager.getConnection(url, properties); 
                                          // Create connection




TYPE 4:
Connecting to a data source using the DriverManager interface with the IBM Data Server Driver for JDBC and SQLJ

A JDBC application can establish a connection to a data source using the JDBC DriverManager interface, which is part of the java.sql package.

The steps for establishing a connection are:

    Load the JDBC driver by invoking the Class.forName method.
If you are using JDBC 4.0 or later, you do not need to explicitly load the JDBC driver.
For IBM Data Server Driver for JDBC and SQLJ, you load the driver by invoking the Class.forName method with the following argument:

com.ibm.db2.jcc.DB2Driver

The following code demonstrates loading the IBM Data Server Driver for JDBC and SQLJ:

try {
  // Load the IBM Data Server Driver for JDBC and SQLJ with DriverManager
  Class.forName("com.ibm.db2.jcc.DB2Driver");
} catch (ClassNotFoundException e) {
     e.printStackTrace();
}



The catch block is used to print an error if the driver is not found.

2. Connect to a data source by invoking the DriverManager.getConnection method.
You can use one of the following forms of getConnection:

getConnection(String url);
getConnection(String url, user, password);
getConnection(String url, java.util.Properties info); 

For IBM Data Server Driver for JDBC and SQLJ type 4 connectivity, the getConnection method must specify a user ID and password, through parameters or through property values.

The url argument represents a data source, and indicates what type of JDBC connectivity you are using.
The info argument is an object of type java.util.Properties that contains a set of driver properties for the connection.

THERE ARE SEVERAL WAYS TO SPECIFY A USERNAME/PASSWORD for connection


Example: Establishing a connection and setting the user ID and password in a URL:

String url = "jdbc:db2://myhost:5021/mydb:" +
  "user=dbadm;password=dbadm;";

                                          // Set URL for data source
Connection con = DriverManager.getConnection(url); 
                                          // Create connection

Example: Establishing a connection and setting the user ID and password in user and password parameters:

String url = "jdbc:db2://myhost:5021/mydb";
                                          // Set URL for data source
String user = "dbadm";
String password = "dbadm";
Connection con = DriverManager.getConnection(url, user, password); 
                                          // Create connection

Example: Establishing a connection and setting the user ID and password in a java.util.Properties object:

Properties properties = new Properties(); // Create Properties object
properties.put("user", "dbadm");         // Set user ID for connection
properties.put("password", "dbadm");     // Set password for connection
String url = "jdbc:db2://myhost:5021/mydb";
                                          // Set URL for data source
Connection con = DriverManager.getConnection(url, properties); 
                                          // Create connection



IBM Data Server Driver for JDBC and SQLJ type 4 connectivity URL option descriptions

The parts of the URL have the following meanings:

jdbc:db2: or jdbc:db2j:net:
    The meanings of the initial portion of the URL are:

    jdbc:db2:
        Indicates that the connection is to a DB2 Database for Linux, UNIX, and Windows.

    jdbc:db2j:net:
        Indicates that the connection is to a remote IBM Cloudscape server.

server
    The domain name or IP address of the data source.
port
    The TCP/IP server port number that is assigned to the data source. This is an integer between 0 and 65535. The default is 446.

property=value;
    A property and its value for the JDBC connection. You can specify one or more property and value pairs. Each property and value pair, including the last one, must end with a semicolon (;). Do not include spaces or other white space characters anywhere within the list of property and value strings



If the database version to which your applications are connecting is later than the database version with which the driver was shipped, the applications cannot use features of the later database version.

====JCC driver Version====
\\
**How to find out the JCC driver version being used in your application** \\
\\
<code>
% db2jcc -version
red 278 % source ~db2leduc/sqllib/db2cshrc
red 280 % db2jcc -version
IBM DB2 JDBC Universal Driver Architecture 3.65.77

Alternatively

red 281 % java com.ibm.db2.jcc.DB2Jcc -version
IBM DB2 JDBC Universal Driver Architecture 3.65.77

red 283 % /cs/local/bin/java -cp /cs/local/pkg/db2/V10.1/java/db2jcc.jar com.ibm.db2.jcc.DB2Jcc -version
IBM DB2 JDBC Universal Driver Architecture 3.65.77


How to display the version of the Universal Driver

red 282 % /cs/local/bin/java -cp /cs/local/pkg/db2/V10.1/java/db2jcc4.jar com.ibm.db2.jcc.DB2Jcc -version
IBM Data Server Driver for JDBC and SQLJ 4.15.82
software/db2/db2_jdbc_drivers.1364819724.txt.gz · Last modified: by seela