HELP!? SQLException: Invalid handle

Hi friends,

I am facing a problem in my program with ODBC-SQL Server 2000.

The program originally ran smooth with one SELECT statement. But I modified the class and added a method with a new SELECT statement, the error appears.

The old SELECT statement works but the new one gives the error. What should I do to give more than one SELECT statements in a class?

Thank a lot.

Diffserv

PS: My code, (Error occurs in the method getUserInfo)

import java.sql.*;

/* Handle the Login Data */

public class LoginCtlClass

{

final static String myDriver = "sun.jdbc.odbc.JdbcOdbcDriver";

static Connection con = null;

String dSN;

String dataSource;

String dataLogin;

String dataPassword;

String infoTable;

String usernameField;

String passwordField;

Statement thzStatement;

ResultSet thzRS;

LoginCtlClass(String myDSN, String myDataLogin, String myDataPassword, String myInfoTable, String myUsernameField, String myPasswordField)

{

dSN = myDSN;

dataSource = "jdbc:odbc:" + dSN;

dataLogin = myDataLogin;

dataPassword = myDataPassword;

infoTable = myInfoTable;

usernameField = myUsernameField;

passwordField = myPasswordField;

}

public int open()

{

try

{

Class.forName(myDriver);

con = DriverManager.getConnection(dataSource, dataLogin, dataPassword);

thzStatement = con.createStatement();

}

catch(java.lang.ClassNotFoundException e)

{

System.err.print("ClassNotFoundException: ");

System.err.println(e.getMessage());

return -1;

}

catch(Exception e)

{

System.err.println(e.getMessage());

return -2;

}

return 0;

}

public void close()

{

try

{

thzStatement.close();

con.close();

}

catch(Exception e)

{

System.err.println("LoginCtlClass Close Error: " + e);

}

}

public boolean pass(String myUsername, String myPassword)

{

String thzSQL;

int noOfUser = 0;

try

{

thzSQL = "SELECT count(*) as noOfUser FROM " + infoTable + " WHERE " + usernameField + "='" + myUsername + "' AND " + passwordField + "='" + myPassword + "'";

thzRS = thzStatement.executeQuery(thzSQL);

if (thzRS.next())

{

noOfUser = thzRS.getInt("noOfUser");

}

thzRS.close();

}

catch(SQLException ex)

{

System.err.println("SQLException in pass(): " + ex.getMessage());

return false;

}

catch(Exception e)

{

System.err.println("LoginCtlClas Error: " + e);

return false;

}

if (noOfUser <= 0)

{

return false;

}

else

{

return true;

}

}

public String getUserInfo(String myUsername, String myPassword, String myInfoField)

{

String thzSQL;

String sqlResult = "";

try

{

thzSQL = "SELECT " + myInfoField + " as SQLresult FROM " + infoTable + " WHERE " + usernameField + "='" + myUsername + "' AND " + passwordField + "='" + myPassword + "'";

thzRS = thzStatement.executeQuery(thzSQL);

System.err.println(thzSQL);

if (thzRS.next())

{

sqlResult = thzRS.getString("SQLresult");

}

thzRS.close();

}

catch(SQLException ex)

{

System.err.println("SQLException in getUserInfo: " + ex.getMessage() + " / " + ex.toString());

return "<ERROR>";

}

catch(Exception e)

{

System.err.println("LoginCtlClas Error: " + e);

return "<ERROR>";

}

return sqlResult;

}

}

[3771 byte] By [diffservhk] at [2007-9-26 4:34:52]
# 1

Hi !

I don't know if this helps you, but I open and close the connection to the db between each query/update.

Like this:

openConnection();

try

{

m_statement.executeUpdate(update);

}

catch(SQLException sqle)

{

sqle.printStackTrace();

}

closeConnection();

/**

* Opens the connection to the database.

*/

protected void openConnection()

{

printlnDebug(9, "openConnection()", "-->");

try

{

m_connection = DriverManager.getConnection(m_dbURL, m_userId, m_passwd);

log("Connection established");

m_statement = m_connection.createStatement();

}

catch(SQLException sqle)

{

log("Connection failed");

sqle.printStackTrace();

}

printlnDebug(9, "openConnection()", "<--");

}

/**

* Close the connection to the database.

*/

protected void closeConnection()

{

printlnDebug(9, "closeConnection()", "-->");

try

{

m_statement.close();

m_connection.close();

}

catch(SQLException sqle)

{

sqle.printStackTrace();

}

printlnDebug(9, "closeConnection()", "<--");

}

Hope it helps...let me know.

qdasson at 2007-6-29 17:51:32 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

I had a similar problem using JDBC amd IBM DB2 Database. I was not able to perform more than one query to the database at a time. It was because JDBC client drivers were not the same version than database.

If it helps you, check your ODBC Drivers, just to see if they are at the same version level than Database you've installed.

vissi at 2007-6-29 17:51:32 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3
According to you, I have to open, query and close again at each query, do I? It seems stupid....Anyway, Thank you very much for help.Teki
diffservhk at 2007-6-29 17:51:32 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 4

I didn't say that you had to do it, it was just an example...

Just to clarify, I didn't mean that you should do the open/close thing every time you do a query/update.

What I meant was that I do this in each method I call, then maybe I do several updates in this method.

And this works fine for me...

qdasson at 2007-6-29 17:51:32 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...