| 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: 16482
|
|
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.)
|
 |
 |
|
|
subject: How to close ResultSet in a function after returning it
|
|
|