Solved

Quering DB2

Posted on 2003-11-18
9
1,370 Views
Last Modified: 2011-10-03
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

0
Comment
Question by:chambers777
  • 4
  • 3
9 Comments
 
LVL 2

Expert Comment

by:SuperKarateMonkey
Comment Utility
Show the code for both the JSP and the main class.
0
 

Author Comment

by:chambers777
Comment Utility
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;
      }
}
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
Comment Utility
Please show the exact syntax in the JSP.  Not all the code is required, obviously, just the part pertaining to the Bean.
0
 

Author Comment

by:chambers777
Comment Utility
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
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:chambers777
Comment Utility
Thanks for your help anyway!
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
Comment Utility
Yup.  Glad it worked out.
0
 
LVL 2

Expert Comment

by:SuperKarateMonkey
Comment Utility
Deletion of the question is fine.  He figured it out for himself.
0
 
LVL 2

Accepted Solution

by:
Lunchy earned 0 total points
Comment Utility
PAQed, with points refunded (50)

Lunchy
Friendly Neighbourhood Community Support Admin
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 75
parentbit challenge 3 50
strCount chalenge 3 50
best (free) software to access postgres db (java) 1 17
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now