aspose file tools*
The moose likes JDBC and the fly likes How to close ResultSet in a function after returning it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "How to close ResultSet in a function after returning it " Watch "How to close ResultSet in a function after returning it " New topic
Author

How to close ResultSet in a function after returning it

Jai Kshir
Greenhorn

Joined: Aug 10, 2007
Posts: 8
This may sound unrealistic but I am getting an error for below function when I apply strict checkcode on my project, Error -

Ensure that resources like this ResultSet object are closed after use

My function looks like -



How do I close this resultSet after I return it ? ...wierd, but I think there is something that could be done...may be different approach..please let me know.


Sip JAVA & Code JAVA
Jai Kshir
Greenhorn

Joined: Aug 10, 2007
Posts: 8
Figured out a way -
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18651
    
    8

That certainly achieves the goal of closing the ResultSet. Unfortunately, as you're going to find out when you run it, it also has the side effect of returning a closed ResultSet to the caller.

If you want to follow the suggestion given by that product, then the approach you should follow is to not return a ResultSet. Instead, inside the getResults() method you should read through the ResultSet and construct a List<Data>, where Data is a class whose instances each hold one row of the ResultSet. Then return that list to the caller. (And keep that bit about closing the ResultSet in the finally block, that's a good thing.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to close ResultSet in a function after returning it