How the JSP connects to the DB2 database

Source: Internet
Author: User
Tags db2 driver manager stmt
The db2 tutorial I am looking at is: How does JSP connect to the DB2 database. JSP and DB2 database connection problems There is 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 through research, now I will publish a little bit, with Jun shared.

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

<html>
<head>
</head>
<body>
<%

String url="jdbc:db2:ch"; //This format is jdbc: subprotocol: subname, where ch is the database name
String user="db2inst1"; / / database connector ID
String password="db2inst1"; / / database linker password
DriverManager.registerDriver(new COM.ibm.db2.jdbc.app.DB2Driver());
// The most important thing is this sentence. DB2 is the same as ORACLE. It is best to create a driver instance explicitly and register it with the driver 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 rs=stmt.executeQuery(sql);
%>
<table border=1 cellspacing=1 cellpadding=0 >
<%
While(rs.next()) { //determine whether the record set tail
%>
<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{ // always close the link, regardless of whether there is an error
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.