Connect compatibility issues for different versions of DB2 databases using JDBC

xiaoxiao2021-03-06  54

DB2 and JDBC support

According to JDBC specification, there are four types of JDBC driver architectures:

TYPE 1: This type of driver implements the JDBC API as a map to another data to access the API, such as Open Database Connectivity (ODBC). Such drivers usually rely on native libraries, which limits its portability. The JDBC-ODBC bridge driver is the most common example of the Type 1 driver. TYPE 2: This type of driver section is written in Java programming language, and part is written in this unit code. These drivers use the local client library specific to the connected data source. Similarly, due to the use of this unit, the portability is limited. TYPE 3: This type of driver uses a pure Java client and communicates with the middleware server using protocols independent of the database, and then the middleware server requests the client request to the data source. TYPE 4: This type of driver is pure Java to implement network protocols for specific data sources. The client is directly connected to the data source.

For DB2 UDB V7.2, it does not support Type 1 and Type 4 drivers, but it provides a driver that supports Type 2 and Type 3, respectively.

Examples are as follows:

Both drivers are provided with DB2JAVA.ZIP with product installation.

Com.ibm.db2.jdbc.app.db2driver

This is a JDBC driver for Type 2, which creates and connects to the DB2 local database or remote database through the DB2 local client library (Copy remote database to local). Therefore, we must deploy DB2 local client libraries simultaneously on the machine where the application system is located, which may be its biggest deficiencies.

The format is used as follows:

Driver name: com.ibm.db2.jdbc.app.db2driver

URL PATTERN: JDBC: DB2: DatabaseName

DatabaseName: The database name that needs to be accessed

Com.ibm.db2.jdbc.net.db2driver

This is a JDBC driver of Type 3, which is established and connected to the DB2 remote database via machine communication that has deployed a DB2 local client library.

The format is used as follows:

Driver name: com.ibm.db2.jdbc.net.db2driver

URL PATTERN: JDBC: DB2: Serverip: DatabaseName

Serverip: The database needs to be accessed in the machine IP address

DatabaseName: The database name that needs to be accessed

(Target DB2 system listens to the service to the default port 6789, otherwise you need to specify the target port number in the URL Pattern)

For DB2 UDB V8.1, it still does not support Type 1 drivers. At the same time, based on DB2 UDB V8.1, it adds support for Type 4 drivers.

Examples are as follows:

DB2 UDB V8.1 still supports two drivers supported above, providing DB2JAVA.ZIP as described above, but the package is different from DB2 UDB V7.2 product, so there may be The compatibility issues for experimental verification below will be made below.

In addition to com.ibm.db2.jdbc.app.db2driver, DB2 UDB V8.1 also provides another Type 2 driver with product installation by db2jcc.jar. It is the presentation of the package is com.ibm.db2.jcc.db2driver, in the DB2 UDB V8.1 initial implementation, this driver is only used to connect directly to the DB2 server using the TYPE 4 driver architecture. The driver receives developers since the DB2 local client library is not required to deploy DB2 local client libraries and performance relatively good. Since DB2 UDB V8.1.2 (installed FixPack 2), developers can also use the driver in the Type 2 architecture to improve the performance of the local application. Here, the two drivers have the same implementation class name, there are two different ways to distinguish which drivers that the DB2 system will eventually instantiate internally:

Take a day, it turned out to be a driver problem, depressed

Complete code: import java.sql. *; Public class test {public test () {}

Public static void main (string [] args) {try {class.forname ("com.ibm.db2.jcc.db2driver). newinstance (); string url =" JDBC: DB2: //10.64.1.202: 50000 / QiCC_DB "; string user =" WPL "; string password =" 111 ";

Connection conn = drivermanager.getConnection (URL, User, Password); statement stmt = conn.createstatement (ResultSet.Type_Scroll_Sensitive, ResultSet.concur_updata);

String SQL = "Select * from xz_gdzc"; ResultSet RS = Stmt.executeQuery (SQL); while (rs.next ()) {system.out.println (rs.getstring (1)); system.out.println (RS .getstring (2));}} catch (exception ex) {system.out.println (ex);}

}

转载请注明原文地址:https://www.9cbs.com/read-115115.html

New Post(0)