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
Error:[IBM][JDBC Driver] CLI0601E Invalid statement handle or statement is closed. SQLSTATE=S1000
Using the DB2JAVA.jar driver.
Thanks
Show the code for both the JSP and the main class.
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.DB2D river";
private String dbURL = "jdbc:db2://sliudv36.co.co m:61342/me ds";
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). newInstanc e();
con = DriverManager.getConnectio n(dbURL, username, password);
} catch (ClassNotFoundException cnfe) {
error = "ClassNotFoundException: Could not locate DB driver.";
throw new ClassNotFoundException(err or);
} 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("Succes sfully connected.");
ResultSet rs = a.executeSelect("SELECT * FROM DEPARTMENT;");
while(rs.next()) {
System.out.println(rs.getS tring("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(queryStr ing);
} 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;
}
}
import java.io.*;
import java.util.*;
import java.sql.*;
public class QueryBean{
private String driverName = "COM.ibm.db2.jdbc.net.DB2D
private String dbURL = "jdbc:db2://sliudv36.co.co
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).
con = DriverManager.getConnectio
} catch (ClassNotFoundException cnfe) {
error = "ClassNotFoundException: Could not locate DB driver.";
throw new ClassNotFoundException(err
} 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("Succes
ResultSet rs = a.executeSelect("SELECT * FROM DEPARTMENT;");
while(rs.next()) {
System.out.println(rs.getS
}
a.disconnect();
}catch(Exception e){
System.out.println("Error:
}
}
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(queryStr
} 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.
ASKER
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
LC
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.