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.
<