How to Use jdbc to connect to a db2 database in jsp

Source: Internet
Author: User

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.

Related Article

Contact Us

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.