JDBC Connection DB2 Database detailed

Source: Internet
Author: User
Tags copy db2 db2 client ibm db2 zip client port number tomcat
Data | database | Detailed from:richardchoi (Richard in Jlubbs)

E-mail:richardchoi@126.com



About DB2 database JDBC Connection article has many, the more famous has such as "the JDBC Database connection encyclopedia" and "the JSP DB2 Connection database", although all is very detailed information, also all said solves the predecessor not to solve the problem, but still has many flaws. I've been writing an article about JDBC Connection database for the last two days to address some people's questions.

First, JDBC is part of the JDK (at least in Java Tiger Development kits), using JDBC to write import java.sql.* directly in the program file;

Second, the format of the connection string. The connection string for the local connection is formatted as jdbc:product_name:database_name and the remote connection is in the format of 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 instance of DB2), and the remote connection string is jdbc:db2://192.168.1.10/50000: RDB (here 192.168.1.10 is the server IP address for the database, and 50000 is the port number for the DB2 connection service).

Third, install the class library for JDBC provided by the DB2 database (called provider in. NET, not yet studied in Java). Find the installation directory for IBM DB2 UDB or the JDK directory for Java Tiger you'll find Db2java.zip, make a copy of it first and then it's most useful. Now we're going to start talking about the program code for the database connection.

Application Connection DB2 Database:

First unzip the db2java.zip, transfer the COM directory to the current directory of the code, and then we start registering the provider instance with the code:

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

Class.forName ("COM.ibm.db2.jdbc.net.DB2Driver"). Newinstance ():

The two sentences are optional, the effect is slightly different, the former is a provider instance with DB2 client, the latter is a provider instance without DB2 client.

Then write connection con=drivermanager.getconnection (), and you can get an instance of the database connection.

Connect DB2 database in JSP:

In this case, Tomcat is used as a servlet container, and if you want to use DB2 provider in Tomcat, you must rename Db2java.zip to Db2java.jar and then copy it to the Common\lib directory under the Tomcat home directory. Restart Tomcat This completes the provider registration. Then the code to register the provider instance is equivalent to the above. There are also articles that are written to another method of registering a provider instance:

Drivermanager.registerdriver (New COM.ibm.db2.jdbc.app.DB2Driver ());

Drivermanager.registerdriver (New COM.ibm.db2.jdbc.net.DB2Driver ());

Database connection instantiation Method Ibid.

In-depth discussion:

I'm not really familiar with Tomcat's operating mechanism and principles, but testing finds that connecting directly from a JSP page to the DB2 database can be very slow. After practice 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, very convenient, but also easy to focus on the code problems. and DB2 database connection will always appear DB2 server itself suddenly down or appear connection more slowly, this is DB2 instance and database parameter configuration improper and cause, I'll be in detail in another article, hope to help everyone.



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.