How to better understand the DB2 driver type

Source: Internet
Author: User
Tags db2 client db2 connect db2 driver ibm database

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:

 
 
  1. import java.sql.*;  
  2. public class Employee {  
  3. /* 
  4. * @param args  
  5. */  
  6. public static void main(String[] args) {  
  7. // TODO Auto-generated method stub  
  8. Employee ee = new Employee();  
  9. 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.

 
 
  1. } catch (Exception e) {  
  2. e.printStackTrace();  
  3. }  
  4. }  

Content navigation

The test driver architecture is type 4 Driver: com. ibm. db2.jcc. DB2Driver, which is located in the package db2jcc. jar.

 
 
  1. @throws InstantiationException  
  2. @throws IllegalAccessException  
  3. @throws ClassNotFoundException  
  4. @throws SQLException  
  5. public void testJcc() throws InstantiationException,  
  6. IllegalAccessException, ClassNotFoundException, SQLException {  
  7. Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();  
  8. String url = "jdbc:db2://192.168.0.17:50000/SAMPLE";  
  9. Connection con = DriverManager.getConnection(url, "db2admin", "cherub");  
  10. con.setAutoCommit(false);  
  11. Statement stmt = con.createStatement();  
  12. ResultSet rs = stmt.executeQuery("select from Employee");  
  13. while (rs.next()) {  
  14. System.out.println(rs.getString(1) + "," + rs.getString(2));  
  15. }  
  16. }  

The test driver architecture is type 3 Driver: COM.ibm.db2.jdbc.net. DB2Driver, which is located in the package db2java.zip.

 
 
  1. @throws InstantiationException  
  2. @throws IllegalAccessException  
  3. @throws ClassNotFoundException  
  4. @throws SQLException  
  5. public void testNet() throws InstantiationException,  
  6. IllegalAccessException, ClassNotFoundException, SQLException {  
  7. 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.

 
 
  1. String url = "jdbc:db2:192.168.0.17:SAMPLE";  
  2. Connection con = DriverManager.getConnection(url, "db2admin", "cherub");  
  3. con.setAutoCommit(false);  
  4. Statement stmt = con.createStatement();  
  5. ResultSet rs = stmt.executeQuery("select from Employee");  
  6. while (rs.next()) {  
  7. System.out.println(rs.getString(1) + "," + rs.getString(2));  
  8. }  
  9. }  

The test driver architecture is type 2 driver: COM. ibm. db2.jdbc. app. DB2Driver, which is also located in the package db2java.zip.

 
 
  1. @throws InstantiationException  
  2.  
  3. @throws IllegalAccessException  
  4.  
  5. @throws ClassNotFoundException  
  6.  
  7. @throws SQLException  
  8.  
  9. public void testApp() throws InstantiationException,  
  10. IllegalAccessException, ClassNotFoundException, SQLException {  
  11. Class.forName("COM.ibm.db2.jdbc.app.DB2Driver").newInstance();  
  12. String url = "jdbc:db2:sample";  
  13. Connection con = DriverManager.getConnection(url, "db2admin", "cherub");  
  14. con.setAutoCommit(false);  
  15. Statement stmt = con.createStatement();  
  16. ResultSet rs = stmt.executeQuery("select from Employee");  
  17. while (rs.next()) {  
  18. System.out.println(rs.getString(1) + "," + rs.getString(2));  
  19. }  
  20. }  
  21. }  

The above content is a comprehensive introduction to the DB2 driver type, and I hope you will gain some benefits.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.