Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a ResultSet from being invalidated on Connection close?

I'd like to pass out a result set from a function that executes a query and closes the connection.

But the ResultSet gets invalidated as soon as its parent Connection is closed and throws

java.sql.SQLException: Operation not allowed after ResultSet closed

How to avoid this?

like image 495
Ivan Avatar asked Dec 01 '25 08:12

Ivan


2 Answers

You can't. If you want to get all the data, loop the ResultSet and insert the data into a collection of yours.

Take a look at commons-dbutils - it has a lot of useful helpers.

like image 88
Bozho Avatar answered Dec 03 '25 22:12

Bozho


Also consider using a disconnected result set: CachedRowSet. Quick googling gave me this: Using CachedRowSet to Transfer JDBC Query Results Between Classes.

like image 28
pedrofurla Avatar answered Dec 03 '25 23:12

pedrofurla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!