Tips for correctly connecting to the DB2 database using JDBC

Source: Internet
Author: User
Tags db2 client ibm db2

This article mainly describes the actual operations on connecting JDBC to the DB2 database. We all know that there are many articles on the actual operations on the JDBC connection of the DB2 database, among them, the famous ones are "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 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. I have written an article about JDBC connection to the database over the past two days to solve some people's questions.

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: // rdb ( 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 It will be most useful after you make a copy of it. Now we will discuss the program code for connecting the database to the DB2 database.

Applications connect to the DB2 database:

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

Class. forName ("COM. ibm. db2.jdbc. app. DB2Driver"). newInstance ();

Class. forName (" 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 as db2java. jar is copied to the common \ lib directory in the tomcat main directory, and Tomcat is restarted 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:

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

DriverManager. registerDriver (new DB2Driver ());

The database connection instantiation method is the same as above.

In-depth discussion:

I am not very familiar with the running mechanism and principle of Tomcat, but it is very slow to directly connect to the DB2 database from the JSP page through testing. After practice, we found that the best way is to use the database connection code in the Servlet and then return the database connection to the JSP page, which is very convenient and easy to solve code problems in a centralized manner. In addition, the DB2 database connection will always show that the DB2 server suddenly goes Down or the connection slows Down, which is caused by improper configuration of DB2 instances and database parameters, I will elaborate on it in another article, hoping to help you.

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: 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.