The following articles mainly explain how to solve the problem of connecting JSP to the DB2 database. We all know that there is a lot of information on the Internet concerning the connection between JSP and the DB2 database, however, I found that most of the data was incorrect and could not really deal with the problem of connecting to DB2. After studying and solving this problem, 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";
Password of the database to connect to the DB2 database
- DriverManager.registerDriver(new COM.ibm.db2.jdbc.app.DB2Driver());
DB2, like ORACLE, 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 rs=stmt.executeQuery(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>
Retrieve 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{
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. The above content describes how to solve the problem of connecting JSP to the DB2 database. I hope it will help you in this regard.