The following articles mainly introduce the full contact of the DB2 driver types. We all know the related data access APIs provided by different versions of the database product line of IBM. It can be said that there are many data access APIs, these include early CLI-based Drivers, recent pure Java drivers, and combinations of CLI and Java.
For a specific DB2 version, the driver has a specific driver and a general driver, which is obvious. however, the relationship between the database systems of many versions and the access drivers of many versions is complicated for customers.
Therefore, when using IBM database products, you must pay attention to the version correspondence and selection of appropriate generic drivers. fortunately, IBM has begun to unify and pay attention to ease of use. These efforts have started from DB2 UDB V7. since then, according to the JDBC driver type in the Java field, IBM has three types of drivers.
The jdbc type 2 driver of DB2 enables Java applications to call db2through JDBC. The call to DB2 JDBC type 2 driver is converted to a Java local method. java applications using this driver must run on a DB2 client, and JDBC requests are forwarded to the DB2 server through this client.
Before using the DB2 JDBC application driver to access the data source in the DB2 UDB for iSeries or DB2 for OS/390 or z/OS environment, you must install DB2 Connect Version 8. DB2 JDBC type 2 driver supports most of the JDBC and SQLJ functions described in the JDBC 1.2 specification, and supports some functions described in the JDBC 2.0 specification.
DB2 jdbc type 3 driver, also known as applet or net driver, consists of a JDBC client and a JDBC server (the process name is db2jd. it can be used in the Applet or in the application.
DB2's jdbc type 4 driver is called a universal driver and implemented in Java only. This driver also supports SQLJ.
Currently, no JDBC driver for TYPE 1 is provided by IBM.
The packages of these drivers are: db2jcc. jar and sqlj.zip files, sqllib \ bin \ db2jcct2. dll (this file is required by type2 ).
Use
When connecting DB2 UDB V7.2 and DB2 UDB V8.1 using JDBC, if you encounter some confusing problems, you can check the JDK used in the development environment, switching from a known JDK provided by IBM (with versions 1.3 and 1.4) often solves your problem.
In the DB2 UDB V8.2 Personal Edition, the following test code Tests Three connection methods and provides the results:
- import java.sql.*;
- public class Employee {
- /*
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Employee ee = new Employee();
- try {
Ee. testJcc (); // succeeded
Ee. testNet (); // succeeded
Ee. testApp (); // the error message returned when the DB2 access client needs to be installed for Type 2. if you want to access the remote database through type 2, you can catalog the remote database locally.
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
Content navigation
The test driver architecture is type 4 Driver: com. ibm. db2.jcc. DB2Driver, which is located in the package db2jcc. jar.
- @throws InstantiationException
- @throws IllegalAccessException
- @throws ClassNotFoundException
- @throws SQLException
- public void testJcc() throws InstantiationException,
- IllegalAccessException, ClassNotFoundException, SQLException {
- Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
- String url = "jdbc:db2://192.168.0.17:50000/SAMPLE";
- Connection con = DriverManager.getConnection(url, "db2admin", "cherub");
- con.setAutoCommit(false);
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("select from Employee");
- while (rs.next()) {
- System.out.println(rs.getString(1) + "," + rs.getString(2));
- }
- }
The test driver architecture is type 3 Driver: COM.ibm.db2.jdbc.net. DB2Driver, which is located in the package db2java.zip.
- @throws InstantiationException
- @throws IllegalAccessException
- @throws ClassNotFoundException
- @throws SQLException
- public void testNet() throws InstantiationException,
- IllegalAccessException, ClassNotFoundException, SQLException {
- Class.forName("COM.ibm.db2.jdbc.net.DB2Driver").newInstance();
// The target DB2 system listens on the default port 6789. Otherwise, you must specify the target port number in URL Pattern.
- String url = "jdbc:db2:192.168.0.17:SAMPLE";
- Connection con = DriverManager.getConnection(url, "db2admin", "cherub");
- con.setAutoCommit(false);
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("select from Employee");
- while (rs.next()) {
- System.out.println(rs.getString(1) + "," + rs.getString(2));
- }
- }
The test driver architecture is type 2 driver: COM. ibm. db2.jdbc. app. DB2Driver, which is also located in the package db2java.zip.
- @throws InstantiationException
-
- @throws IllegalAccessException
-
- @throws ClassNotFoundException
-
- @throws SQLException
-
- public void testApp() throws InstantiationException,
- IllegalAccessException, ClassNotFoundException, SQLException {
- Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();
- String url = "jdbc:db2:sample";
- Connection con = DriverManager.getConnection(url, "db2admin", "cherub");
- con.setAutoCommit(false);
- Statement stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery("select from Employee");
- while (rs.next()) {
- System.out.println(rs.getString(1) + "," + rs.getString(2));
- }
- }
- }
The above content is a comprehensive introduction to the DB2 driver type, and I hope you will gain some benefits.