Link to home
Start Free TrialLog in
Avatar of Yuanyu
Yuanyu

asked on

Connect to DB2

In my Linux platform, I need to connect to a DB2 database under OS400 with JSP, could you give me a solution?
ASKER CERTIFIED SOLUTION
Avatar of kotan
kotan
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Yuanyu
Yuanyu

ASKER

Does the second way means:
57xxJC1 is in the list LICPGM, we can connect to DB2/OS400 from Linux or Windows platform without any driver installion?  
yes! The driver is already included in IBM Toolbox.
Avatar of Mick Barry
One of IBM's red books may help you:

http://www.as400.ibm.com/developer/java/books.html
Avatar of Yuanyu

ASKER

OS/400 version is V4R2, can I use toolbox 57xxJC1?
once you have your jdbc driver downloaded.. include it in your servlet container's environment as a standard lib.

you can go here to find out how to use JDBC to connect to your DB:
http://www7b.boulder.ibm.com/dmdd/library/techarticle/0203zikopoulos/0203zikopoulos.html [overview on db2 and jdbc]

http://www-105.ibm.com/developerworks/education.nsf/java-onlinecourse-bytitle/5D92193F478974D386256B210044A302?OpenDocument [tutorial about using jdbc from a jsp to connect to a DB, registration is free]

http://www-106.ibm.com/developerworks/library/connecting-enterprise-data/enterprise-data.html [how to access db2 data using jdbc in java/jsp]

here is a redbook on it:
http://www.redbooks.ibm.com/redpieces/pdfs/sg246551.pdf

if you can't find the IBM Supplied JDBC driver here is an alternative one:
http://www.hitsw.com/support/dloadsqljdbc400top.htm

here is how to set up the IBM JDBC driver:
http://as400bks.rochester.ibm.com/pubs/html/as400/v5r1/ic2924/index.htm?info/rzaha/jdbc.htm
http://as400bks.rochester.ibm.com/pubs/html/as400/v5r1/ic2924/index.htm?info/rzaha/setupjdbc.htm

HTH,
CJ
any luck?

CJ
Ok Yuanyu,

I think i am in a better positions to help cause I have just finished my project in JSP on DB2 and OS400. I think u need mor sample code other than looking around in the info center again.

First, I am using JSP and JavaBean to work. To gather info from user, I used JSP to capture user information, after that I used JavaBEan to do the connection. So, for your problems, u need to know how to connect right? Here your are :

********** connect.java ********

1 package com.wrox.cars;
2
3import java.sql.*;
4import java.util.*;
5
6public class Books {
7        String error;
8        Connection con;
9
10        public Books() { }
11
12        public void connect() throws ClassNotFoundException,
13                                     SQLException,
14                                     Exception {
15        try {
16           Class.forName("com.ibm.db2.jdbc.app.DB2Driver");
17           con = DriverManager.getConnection("jdbc:db2:*local");
18        } catch (ClassNotFoundException cnfe) {
19          error = ("ClassNotFoundException: Could Not Locate Db Driver");
20          throw new ClassNotFoundException(error);
21        } catch (SQLException cnfe) {
22          error = ("ClassNotFoundException : Could Not Connect to DB.");
23          throw new SQLException(error);
24        } catch (Exception e) {
25          error = ("Exception : An Unknown error occur.");
26          throw new Exception (error);
27        }
28      }
29
30
31        public void disconnect() throws SQLException {
32           try {
33                if ( con != null) {
34                     con.close();
35                   }
36                } catch (SQLException sqle) {
37                  error = ("SQLEXception : Unable to close the database.");
38                  throw new SQLException (error);
39                }
40              }
41
42
43        public ResultSet viewBooks() throws SQLException, Exception {
44                ResultSet rs = null;
45
46                try {
47                 String queryString =("SELECT TA1001, TA1021, TA1015 FROM lflib.jalf11 WHERE TA1014='C1'");
48                 Statement stmt = con.createStatement();
49                 rs = stmt.executeQuery(queryString);
50              } catch (SQLException sqle) {
51                        error = ("SQLException : Could not execute query");
52                        throw new SQLException (error);
53              } catch (Exception e) {
54                        error = ("Exception : An Unknown error occur.");
55                        throw new Exception (error);
56              }
57
58              return rs;
59       }
60
61       }


The bean above, as u see, quite clear right? 3 methods. One is doing the connection, which is include of all the driver, location of thedb etc... The second fucntions is disconnect, which u need to close all the connectivity to the db before u close the pages. The third one is the query part. U can put any type of SQL command here as long as it's a valid command.

OK??
Hope it's can help.

Oh yeah... regarding your 5722-JV1 or any other licienced programs. As long as it's in the INSTALLED or COMPATIBLE mode, it's can be used. And u no need to specific anythings in order for u to used. No need. U can straight away used this.