Guidance:AboutDB2 databaseOfJDBCThere are many connection articles, such as "JDBC database connection Daquan" and "JSP DB2 Connection database". Although they are all very detailed materials, it is also said that it solves the problems not solved by our predecessors, but there are still many flaws. This article describes how to connect to a DB2 database through JDBC.
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,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:
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 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:
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, the DB2 database connection will always show that the DB2 server suddenly goes Down or the connection slows Down, which is caused by incorrect configuration of DB2 instances and database parameters. I hope this will help you.