Methods for JSP to connect DB2 database with JDBC

Source: Internet
Author: User
Tags db2 connect stmt port number

Database version: DB2 UDB v7.2

Server operating system: aix4.3.3

1, shut down the Web service on the server;
2. Stop the JDBC Listener process DB2JD,DB2JD typically starts with a 6789 port service,
First Use Ps-ef | grep DB2JD to see its process number.
Then stop with kill-9 < process number > command;
3, in the database server, into the/usr/lpp/db2_07_01/java12 directory, execution./USEJDBC2 to enable JDBC2;
4, the/usr/lpp/db2_07_01/java12 under the Db2java.zip copy to their own Web project under the Web-inflib, if the use of Tomcat, the copy of the file renamed to Db2java.jar;
5, on the server, Su-< database instance User name >
Execute DB2JSTRT 6789 (this sentence starts the DB2JD process, 6789 is the default server listens for JDBC2 connection ports, or it can be set to any other conflict-free port. )
6, in the establishment of their own projects, the establishment of a test JSP file, the contents are as follows:
<%@ page contenttype= "text/html; charset=gb2312 "language=" java import= "java.sql.*"%>

      <body>
   The following is the data read from the DB2 database:     <table border=1>
   <%
   class.forname ("COM.ibm.db2.jdbc.net.DB2Driver" ). newinstance ();
   Connection con=java.sql.drivermanager.getconnection ("Jdbc:db2://192.168.168.2:6789/chaodb", " Db2inst1 "," Mima ");
   Statement stmt=con.createstatement ();
    ResultSet rst=stmt.executequery ("Select username from Tname");
    while (Rst.next ())
    {
     out.println ("<tr > ");
     out.println ("<td>" +rst.getstring ("username") + "</td>");
     out.println ("</tr>");
   }
   //closing connections, releasing resources
    rst.close ();
    stmt.close ();
    Con.close ();
   %>   
    </table>
    </body>
   

    Description:
    192.168.168.2 is the IP address of the server;
    6789 is the port number on which JDBC2 listens. With server-side settings consistent, if the server itself is 6789, then the program of this place reference, you can omit this number.
    Chaodb is a database built on the server;
    Db2inst1 is an instance user with a Chaodb database;
     Mima is the password of the instance user db2inst1;
    first set up a table in the database, the name is Tname, and one of the columns is username, input some data into it.

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.