The following articles mainly describe the cracking of some problems that may easily occur when connecting JSP to the DB2 database. If you encounter some problems that may occur when connecting JSP to the DB2 database in actual operations, however, you do not know how to solve the problem correctly, so the following articles must be good teachers and friends for you.
There is a lot of information on the Internet about the connection between JSP and DB2 databases, but I found that most of the information is incorrect and cannot really deal with the connection with DB2. I have solved this problem after research.
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";
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 Database Connection DB2 database 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 introduces how to solve the problem of connecting JSP to the DB2 database.