Link to home
Start Free TrialLog in
Avatar of chambers777
chambers777

asked on

Quering DB2

Why is it that I am able to connect to and query a db2 database through my JSP code but when I execute the same class through the command line, it returns this error?

Error:[IBM][JDBC Driver] CLI0601E  Invalid statement handle or statement is closed. SQLSTATE=S1000

Using the DB2JAVA.jar driver.

Thanks

Avatar of SuperKarateMonkey
SuperKarateMonkey

Show the code for both the JSP and the main class.
Avatar of chambers777

ASKER

Both JSP and main are using the same code..... two classes. MAIN is in the second

import java.io.*;
import java.util.*;
import java.sql.*;


public class QueryBean{
      private String driverName = "COM.ibm.db2.jdbc.net.DB2Driver";
      private String dbURL = "jdbc:db2://sliudv36.co.com:61342/meds";
      private String username = "user";
      private String password = "pass";
      public PreparedStatement ps = null;
      Connection con;
      String error;
      public Statement stmt;
      public ResultSet result;
        public String n;
      
      public QueryBean(){
         
      }
      
      public void connect() throws ClassNotFoundException, SQLException, Exception {
            try {
              Class.forName(driverName).newInstance();
              con = DriverManager.getConnection(dbURL, username, password);
            } catch (ClassNotFoundException cnfe) {
              error = "ClassNotFoundException: Could not locate DB driver.";
              throw new ClassNotFoundException(error);
            } catch (SQLException cnfe) {
              error = "SQLException: Could not connect to database.";
              throw new SQLException(error);
            } catch (Exception e) {
              error = "Exception: An unknown error occurred while connecting to database.";
              throw new Exception(error);
            }
      }
      
      public void disconnect() throws SQLException {
            try {
              if ( con != null ) {
                  con.close();
              }
            } catch (SQLException sqle) {
              error = ("SQLException: Unable to close the database connection.");
              throw new SQLException(error);
            }
      }
}

import java.text.SimpleDateFormat;
import java.sql.*;
import java.util.*;
import java.io.*;

public class MedsQB extends QueryBean{
      String queryString = null;
      
      public static void main(String args[]){
            MedsQB a = new MedsQB();
            
            try{
                  a.connect();
                  System.out.println("Successfully connected.");

                  ResultSet rs = a.executeSelect("SELECT * FROM DEPARTMENT;");                  
      
                   while(rs.next()) {
                          System.out.println(rs.getString("div_nm"));
                  }
                  a.disconnect();
            }catch(Exception e){
                  System.out.println("Error: " + e.getMessage());
            }
      }

      public MedsQB(){}
      
      public boolean shutdown(){
            return this.shutdown();
      }

      private ResultSet executeSelect(String queryString) throws SQLException, Exception{
            ResultSet rs = null;
            try{
              Statement stmt = con.createStatement();
              rs = stmt.executeQuery(queryString);
            } catch (SQLException sqle) {
              error = "SQLException: Could not execute the query. Error:" + sqle.getMessage();
              throw new SQLException(error);
            } catch (Exception e) {
              error = "An exception occured while retrieving records.";
              throw new Exception(error);                   
            }
            queryString = null;
            return rs;
      }
}
Please show the exact syntax in the JSP.  Not all the code is required, obviously, just the part pertaining to the Bean.
I've figured out the problem.. I had two driver files in my classpath... removed the older one and I'm good to go!!!
LC
Thanks for your help anyway!
Yup.  Glad it worked out.
Deletion of the question is fine.  He figured it out for himself.
ASKER CERTIFIED SOLUTION
Avatar of Lunchy
Lunchy
Flag of Canada 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