Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to close ResultSet in a function after returning it

 
Jai Kshir
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jai Kshir
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Figured out a way -
 
Paul Clapham
Sheriff
Posts: 21298
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic