aspose file tools*
The moose likes JDBC and the fly likes Resultset is closed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Resultset is closed" Watch "Resultset is closed" New topic
Author

Resultset is closed

jaya nair
Greenhorn

Joined: Aug 17, 2005
Posts: 9
Hi All.

I have an issue with JDBC. I have a Stored Proc that returns both output parameters and a ResultSet, which I am calling through a CallableStatement. This is my code:

callableStmt.execute();
rs = call.getResultSet();

try {
while (callableStmt.getMoreResults()) {
irs = callableStmt.getResultSet();

}
String string = callableStmt.getString(1);

if(resultSet != null)
{
while (resultSet.next())
{
� do something
}
}

When this executes, I get an "Object is closed" exception at resultSet.next(). When I do callableStmt.getString(1) after I finish processing the ResultSet, everything goes well. But there it is mandatory for me to get the output parameter before I process the ResultSet.
I am working on MSSQL Server 2000, using MSSQL drivers. Please tell me if this is a problem with the driver implementation or is a normal behavior of JDBC. Thanks.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

From the API for getResultSet():

Retrieves the current result as a ResultSet object. This method should be called only once per result.

http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Statement.html

I'm assuming the difference between rs and resultSet is just a typo and has no bearing on the problem, otherwise you need to provide more information.

Dave
jaya nair
Greenhorn

Joined: Aug 17, 2005
Posts: 9
Sorry this is the corrected code....can u please suggest why im getting object closed at rs.next(). If i comment rs.getString(1) part ..the code works fine...


callableStmt.execute();
rs = callableStmt.getResultSet();

String string = rs.getString(1);

if(rs!= null)
{
while (rs.next())
{
� do something
}
}
todd runstein
Ranch Hand

Joined: Feb 15, 2005
Posts: 64
Try putting the rs.getString(1) call after rs.next(). You need to get to the first record.

if (rs!=null){
while (rs.next()){
String s = rs.getString(1);
....
}
}

//Don't forget to close!
rs.close();
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30919
    
158

Now would be a good time to note that the resultset can never be null. The driver will return a resultset with no records or throw an exception. So you don't need to check for a null value there.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Resultset is closed