Database Version: db2 UDB v7.2
Server Operating System: aix4.3.3
1. Disable web Services on the server;
2. Stop the jdbc listening process db2jd. db2jd generally starts port 6789,
Use ps-ef | grep db2jd to view its process number.
Run the kill-9 <process no.> command to stop the service;
3. on the database server, go to the/usr/lpp/db2_07_01/java12 directory and run./usejdbc2 to enable jdbc2;
4. Copy db2java.zip under/usr/lpp/db2_07_01/java12to the WEB-INF \ lib of your web project. If tomcat is used, rename the copied file to db2java. jar;
5. On the server, su-<database instance username>
Run db2jstrt 6789 (this statement starts the db2jd process. 6789 is the default port on which the server listens to the jdbc2 connection and can be set to another non-conflicting port .)
6. Create a test jsp file in your own project. The content is as follows:
<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" %>
<Html>
<Body>
The following data is read from the db2 database: <Table border = 1>
<%
Class. forName ("COM.ibm.db2.jdbc.net. DB2Driver"). newInstance ();
Connection con = java. SQL. DriverManager. getConnection ("jdbc: db2: // 192.168.168.2: 6789/chaodb", "db2inst1", "mima ");
Statement stmt = con. createStatement ();
ResultSet rst1_stmt.exe cuteQuery ("select username from tname ");
While (rst. next ())
{
Out. println ("<tr> ");
Out. println ("<td>" + rst. getString ("username") + "</td> ");
Out. println ("</tr> ");
}
// Close the connection and release resources
Rst. close ();
Stmt. close ();
Con. close ();
%>
</Table>
</Body>
</Html>
Note:
192.168.168.2 is the IP address of the server;
6789 is the port number that jdbc2 listens on. It must be set to the same as that on the server. If the server uses 6789, this number can be omitted when the program references this part.
Chaodb is a database built on the server;
Db2inst1 is an instance user with a chaodb database;
Mima is the password of instance user db2inst1;
First, create a table named tname in the database, and enter some data in the column named username.