Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Resultset is closed

 
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.
 
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
}
}
 
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();
 
author & internet detective
Posts: 40200
816
Eclipse IDE VI Editor Java
  • 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.
 
    Bookmark Topic Watch Topic
  • New Topic