The following articles mainly describe the practical operation skills for connecting JDBC to the DB2 database. If you are interested in the actual operation skills for connecting JDBC to the DB2 database, you can click to view the following articles. The following describes the specific solution and hope it will be helpful for your future study.
Database, JDBC, skill database, JDBC, skill
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: // 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 we will discuss the program code for connecting the database to the DB2 database.
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:
- Class.forName(“COM.ibm.db2.jdbc.app.DB2Driver”).newInstance(); 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 instance for connecting the database to the DB2 database.
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 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 COM.ibm.db2.jdbc.net.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, DB2's database connection will always show that the DB2 server suddenly goes Down or the connection to the DB2 database slows Down, which is caused by improper configuration of DB2 instances and database parameters.
The above content introduces the practical operation skills for connecting to the DB2 database through JDBC, and I hope you will get some benefits.