Description of the DB2 JDBC connection method and application code

Source: Internet
Author: User
Tags db2 client ibm db2

This article mainly introduces the description of the correct DB2 JDBC connection operation method and the actual application code. We all know that there are many articles about the JDBC connection of the DB2 database, well-known include "JDBC database connection Daquan" and "JSP DB2 Connection database ".

Although they are all very detailed materials, they all say that they have solved the problems not solved by our predecessors, there are still many leaks.

There are many articles on JDBC connection to DB2 databases, such as "JDBC database connection Daquan" and "JSP DB2 Connection database". Although they are all very detailed information, it is also said that it solves the problems not solved by our predecessors, but there are still many flaws.

First, JDBC is a part of JDK (at least in Java Tiger Development Kits). You can use JDBC to directly write import java. SQL. * In the program file.

Second, the format of the connection string. The format of the local connection string is jdbc: product_name: database_name. The format of the remote connection is jdbc: product_name: // host_name/port _ number: database_name.

That is, if my database name is rdb, the local connection string is jdbc: db2: rdb (of course, rdb must be in the default DB2 instance), and the remote connection string is jdbc: db2: // 192.168.1.10/50000: rdb (192.168.1.10 indicates the IP address of the database server, and 50000 indicates the port number of the DB2 Connection Service ).

Third, install the class library provided by the DB2 database for JDBC (called Provider in. NET, which has not been studied yet in Java ). Find the installation directory of IBM DB2 UDB or the jdkdirectory of Java tigeryou will find db2java.zip. It will be most useful after you make a copy of it. Now let's start to discuss the program code for database connection.

Applications connect to the DB2 database:

First decompress db2java.zip, transfer the COM directory to the current directory of the code, and then register the instance of this Provider. The code is:

 
 
  1. Class.forName(“COM.ibm.db2.jdbc.app.DB2Driver”).newInstance();  
  2. Class.forName(“COM.ibm.db2.jdbc.net.DB2Driver”).newInstance():  

Either of the two statements has a slightly different role. The former is a Provider instance with a DB2 client, and the latter is a Provider instance without a DB2 client.

Then write Connection con = DriverManager. getConnection (); to obtain the database Connection instance.

Connecting to the DB2 database in JSP:

Tomcat is used as the Servlet container. If you want to use DB2 provider.pdf in Tomcat to name db2java.zip into db2java. jar, copy it to the commonlib directory in the tomcat home directory, and restart Tomcat to complete Provider registration. Then the code for registering the Provider instance is the same as above. There is also an article writing to another method to register the Provider instance:

 
 
  1. DriverManager.registerDriver(new COM.ibm.db2.jdbc.app.DB2Driver());  
  2. DriverManager.registerDriver(new COM.ibm.db2.jdbc.net.DB2Driver());  

The database connection instantiation method is the same as above. The above content is an introduction to the DB2 JDBC Connection Method and Application Generation. I hope you will get something.

The above content is an introduction to the DB2 JDBC connection method and application code. 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.