Solve the Problem of connecting JSP to the DB2 database

Source: Internet
Author: User

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.

 
 
  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 a database connection 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 describes how to solve the problem of connecting JSP to the DB2 database. I hope it will help you in this regard.

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.