• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Resultset is closed

 
jaya nair
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34851
369
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic