Developer on Alibaba Coud: Build your first app with APIs, SDKs, and tutorials on the Alibaba Cloud. Read more ＞
Data | database | Detailed from:richardchoi (Richard in Jlubbs)
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:
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.
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.
This article is an English version of an article which is originally in the Chinese language on aliyun.com and is provided for information purposes only. This website makes no representation or warranty of any kind, either expressed or implied, as to the accuracy, completeness ownership or
reliability of the article or any translations thereof. If you have any concerns or complaints relating to the article, please send an email, providing a detailed description of the concern or
complaint, to firstname.lastname@example.org. A staff member will contact you within 5 working days. Once verified, infringing content will be removed immediately.
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.