Demonstration of connecting Java to the DB2 database

Source: Internet
Author: User
Tags db2 client db2 connect db2 driver websphere application server

This article mainly describes the actual operation process of connecting Java to the DB2 database. If you encounter the actual operation process of connecting Java to the DB2 database during actual operations, however, you do not know how to solve the problem correctly, so the following articles must be good teachers and friends for you.

1. About Connection Pool

JDBC itself does not provide connection pool technology and requires support from third-party libraries.

The best Connection Pool for applications that require frequent Connection, such as in Web Application

For general Java applications, you can directly connect to the DB2 database using JDBC.

2. Connect to DB2 in Java Application

After installing DB2 Personal Edition V9, you can find these two files in the java Directory: db2jcc. jar; db2jcc_license_cu.jar

The com. ibm. db2.jcc. DB2Driver driver in db2jcc. jar is called a universal driver. The recommended reason is ). When using it, db2jcc_license_cu.jar is required. Otherwise, a message indicating that the license is not available cannot work.

DB2 connection address: jdbc: db2: // localhost: 50000/Database

3. Connecting to DB2 in Web Application

1. Use WAS as an example to configure DB2 Data Source

1) Go to the console

Start the WebSphere 9060 service, open a browser, and enter http: // localhost:/ibm/console/in the address bar to log on to the WebSphere console.

2) Set Environment Variables

In the left-side Navigation Pane, select Environment-> Websphere Variables, find DB2UNIVERSAL_JDBC_DRIVER_PATH, and enter the location of the DB2 database driver jar package, such as C: \ Program Files \ IBM \ SQLLIB \ java. OK, save.

Note: When you use a management tool to access a remote machine, the set database driver jar package must be the location of the machine where the application server is located, not the location where the local driver is located, otherwise, the service will not be able to load the driver, and the test data source connection to the DB2 database will fail.

3) create a JDBC Provider

In the left-side Navigation Pane, choose Resources> JDBC Providers. Click New. set Database type to DB2; select DB2 Universal JDBC Driver Provider for Provider type; select Connection pool data source for Implementation type. (Note that non-English users may encounter errors, you only need to open the control panel-> region and language options-> the standard and format of the region options can be selected as English (US); Name can be set at will; click Next; Finish; Save.

4) create a Data source

In the left-side Navigation Pane, choose Resources> JDBC> Data sources. click New. set Data source name; Set JNDI name; Next; select the newly created JDBC Provider; Next; set Database name to your Database name; select 4 for Driver type; and set Server name to the ip address of the server; port 50000; Next; Finish; Save.

5) Select the newly created Data source, configure JAAS in Related Items, set Alias as the Alias, set User ID and Password as the User name and Password for accessing DB2, and OK; Save.

6) Go to the previous Data source, and select the newly configured JAAS from Component-managed authentication alias; OK; Save.

7) Select the Data source and click Test connection to Test the Data source.

2. Call Data source in the JEE Project

In the JEE project, you need to obtain the Datasource In the servlet. The method is as follows:

 
 
  1. public class DBConnector {  
  2. public static Connection getConnection() throws SQLException, NamingException {  
  3. Context ctx = new InitialContext();  
  4. DataSource ds = (DataSource) ctx.lookup("jdbc/db2"); 

Jdbc/db2 is in the preceding WAS

 
 
  1. Connection conn = ds.getConnection();  
  2. }  

Call the DBConnector. getConnection () method in the doPost or doGet method of a Servlet.

4. DB2 connection interface

DB2 provides two APIs: JDBC and SQLJ.

SQLJ is a standard development model used to access data from Java applications. The sqlj api is defined in the SQL 1999 specification.

JDBC drivers are divided into the old CLI Driver and the new Universal JDBC Driver ). The new universal JDBC Driver provides support for both JDBC and SQLJ APIs in one implementation. JDBC and SQLJ can operate on each other in the same application.

5. Relationship between JDBC connection to the DB2 database and the DB2 driver

The JDBC driver architecture can be divided into four types: Type1, Type2, Type3, and Type4.

The JDBC Type 1 driver is based on the JDBC-ODBC bridge. Therefore, ODBC drivers can be used together with such JDBC drivers provided by Sun. IBM does not support the Type 1 driver, so it is not a recommended environment.

The JDBC Type 2 driver relies on the operating system-specific library to communicate with RDBMS. The application will mount this JDBC driver, and the driver will use the shared library to communicate with the DB2 server. DB2 provides two different Type 2 drivers:

The old cli jdbc driver is provided in the file db2java.zip. The implementation package name is COM. ibm. db2.jdbc. app. DB2Driver. The alias "app driver" is derived from the concept and package name, which means that the driver will execute local connections through the local DB2 UDB client of the remote database.

The common JDBC driver is provided in the file db2jcc. jar. The implementation package name is com. ibm. db2.jcc. DB2Driver. In the initial implementation V8.1), this driver is used to use the Type 4 driver architecture to directly connect to the DB2 server in Java.

One of the main reasons for using this driver in the Type 2 architecture is for local application performance and distributed transaction support. The general JDBC Type 2 driver uses com. ibm. db2.jcc. DB2XADataSource and com. ibm. db2.jcc. DB2ConnectionPoolDataSource to support distributed transactions and connection pools.

The JDBC Type 3 driver is a pure Java implementation. It must communicate with the DB2 JDBC Applet Server (DB2 JDBC Applet Server) to access DB2 data. This type of driver is designed to enable Java applet to access the DB2 data source.

JDBC Type 3 drivers are often referred to as "Network net) drivers". They are named based on the package name COM.ibm.db2.jdbc.net. The Type 3 Driver requires that the db2java.zip driver is always at the same maintenance level as the DB2 Applet Server. If the driver is used in the applet, this is not a problem, because the browser will download the corresponding db2java.zip file during application execution.

Many customers use the Type 3 driver instead of the Type 2 driver to avoid the necessary DB2 Client installation and required DB2 catalog database commands, the latter is used to create the database directory information required to use the old CLI driver for Type 2 connection. Currently, WebSphere Application Server and other J2EE servers do not support the IBM Type 3 driver because the driver does not support distributed transaction JTA ).

The Type 4 driver is only used for Java JDBC drivers. It connects directly to the DB2 database to the database server. DB2 V8.1 introduces a Type 4 driver called "Universal JDBC driver. The common JDBC driver is provided in the file db2jcc. jar. The implementation package name is com. ibm. db2.jcc. DB2Driver.

Note that the generic Type 2 and generic Type 4 drivers have the same implementation class name. There are two ways to differentiate which driver DB2 will instantiate:

Use different connection URL modes to indicate which of the following actions you want Type 2 and Type 4.

Example of Type 4 URL mode: string "jdbc: db2: // server1: 50000/sample ". The JDBC driver is required to directly connect the Java application to the database named "sample" on the DB2 server, which is located in the DB2 instance configured on the DB2 server host name server1, the DB2 server listens on port 50000.

Example of Type 2 URL mode: string "jdbc: db2: sample ".

Important: for DB2 UDB V8.1.2, the general JDBC driver requires that the CLASSPATH have the license JAR file and the db2jcc. jar file. The following is the required license JAR file:

For Cloudscape™Network Server V5.1: db2jcc_license_c.jar

For DB2 UDB V8 for Linux, UNIX and Windows Servers: db2jcc_license_su.jar

For DB2 UDB for iSeries®And z/OS Server is provided together with DB2 Connect and DB2 Enterprise Server Edition): db2jcc_license_cisuz.jar

6. More

See http://blog.csdn.net/peart_boy/archive/2006/10/16/1336201.aspx

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.