How the JSP connects to the DB2 database _db2

Source: Internet
Author: User
Tags db2 stmt
The DB2 tutorial you are looking at is: How JSP connects to the DB2 database. JSP and DB2 database links to a lot of information on the Internet, but the author found that most of the data are wrong, can not really deal with the problem of DB2 links, the author after the study to solve this problem, I have a little experience published, and June share.

<%@ page session= "false"%>
<%@ page import= "java.sql.*"%>
<%@ page import= "java.util.*"%>

<body>
<%

String url= "Jdbc:db2:ch"; This format is JDBC: Child protocol: Child 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 ());
The most critical of this, 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 (); To create a database connection object
String sql= "SELECT * from Task";
ResultSet rs=stmt.executequery (SQL);
%>
<table border=1 cellspacing=1 cellpadding=0
<%
while (Rs.next ()) {//Determine whether the end of the recordset
%>
<tr>
<td> <%=rs.getstring (1)%> </td>//Remove the value of each column and display
<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{///No matter if there is an error, always close the link
if (conn!=null) {
Conn.close ();
}
}
%>
</table>
<body>

The above program runs on the 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.