From: Richard Choi (Richard in JLUBBS)
E-mail: richardchoi@126.com
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 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 ());