Forums Register Login

How to close ResultSet in a function after returning it

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Figured out a way -
+Pie Number of slices to send: Send
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.)
ice is for people that are not already cool. Chill with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 5135 times.
Similar Threads
Tomcat Connection Pool
Close a resultset in a function while returning the values
ResulSet issue
closing Connection before returning ResultSet
Problem in Seperating Core Servlet to Database Query Java
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 23:01:17.