How does JSP connect to the DB2 database?

Source: Internet
Author: User

The db2 tutorial is: how to connect JSP to the DB2 database. JSP and DB2 database connection problems there are a lot of information on the Internet, but I found that most of the information is wrong, can not really deal with the problem of DB2 connection, the author has solved this problem after research, I will share my experiences with you.

<% @ Page session = "false" %>
<% @ Page import = "java. SQL. *" %>
<% @ Page import = "java. util. *" %>

<Html>
<Head>
</Head>
<Body>
<%

String url = "jdbc: db2: ch"; // This format is jdbc: Sub-Protocol: Sub-name, where ch is the database name
String user = "db2inst1"; // Database Connector ID
String password = "db2inst1"; // Database Connector password
DriverManager. registerDriver (new COM. ibm. db2.jdbc. app. DB2Driver ());
// This sentence is the most important. Like DB2 and ORACLE, it is best to create a drive instance explicitly and register it with the drive manager.
// Other databases generally use Class. forName ("xxxxxxxxxxx ");
Connection conn = null;
Try {
Conn = DriverManager. getConnection (url, user, password );
Statement stmt = conn. createStatement (); // create a database connection object
String SQL = "select * from task ";
ResultSet rs1_stmt.exe cuteQuery (SQL );
%>
<Table border = 1 cellspacing = 1 cellpadding = 0>
<%
While (rs. next () {// judge whether the end of the record set is
%>
<Tr>
<Td> <% = rs. getString (1) %> </td> // retrieves the value of each column and displays
<Td> <% = rs. getString (2) %> </td>
<Td> <% = rs. getString (3) %> </td>
<Td> <% = rs. getString (4) %> </td>
<Td> <% = rs. getString (5) %> </td>
<Td> <% = rs. getString (6) %> </td>
</Tr>
<%}
Rs. close ();
Rs = null;
Stmt. close ();
Stmt = null;
}
Finally {// whether an error occurs or not, always close the link.
If (conn! = Null ){
Conn. close ();
}
}
%>
</Table>
<Body>
<Html>

The above program runs on AIX4.3 + DB27.2 + JDK1.3 + TOMCAT4.1.6.
<

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.