Cracking of some common problems in connecting JSP to the DB2 database

Source: Internet
Author: User

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.

 
 
  1. <%@ page session="false" %>  
  2. <%@ page import="java.sql.*"%>  
  3. <%@ page import="java.util.*"%>   
  4. <html>  
  5. <head>  
  6. </head>  
  7. <body>  
  8. <%  
  9. String url="jdbc:db2:ch"; 

This format is jdbc: Sub-Protocol: Sub-name, where ch is the database name

 
 
  1. String user="db2inst1"; 

Database Connector ID

 
 
  1. String password="db2inst1"; 

Password of the database to connect to the DB2 database

 
 
  1. 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 ");

 
 
  1. Connection conn=null;  
  2. try{  
  3. conn= DriverManager.getConnection(url,user,password);  
  4. Statement stmt=conn.createStatement(); 

Create Database Connection DB2 database object

 
 
  1. String sql="select * from task";  
  2. ResultSet rs=stmt.executeQuery(sql);  
  3. %>  
  4. <table border=1 cellspacing=1 cellpadding=0 >  
  5. <%  
  6. while(rs.next()) { 

Judge whether the end of the record set is

 
 
  1. %>  
  2. <tr>   
  3. <td><%=rs.getString(1)%></td> 

Retrieve the value of each column and display

 
 
  1. <td><%=rs.getString(2)%></td>  
  2. <td><%=rs.getString(3)%></td>  
  3. <td><%=rs.getString(4)%></td>  
  4. <td><%=rs.getString(5)%></td>  
  5. <td><%=rs.getString(6)%></td>  
  6. </tr>  
  7. <%}  
  8. rs.close();  
  9. rs=null;  
  10. stmt.close();  
  11. stmt=null;  
  12. }  
  13. finally{ 

Whether an error occurs or not, always close the link.

 
 
  1. if (conn!=null){  
  2. conn.close();  
  3. }  
  4. }  
  5. %>  
  6. </table>  
  7. <body>  
  8. <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.

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.