Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing Connection object once I got my required ResultSet

Tags:

java

jdbc

Would there be any effect if I close my Connection object once I got my ResultSet?

Connection con= DriverManager.getConnection();
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(query);
con.close();

Further to this I use my rs object and I feel there will not be any impact on it. Because ResultSet and Statement are in connection with each other.

like image 218
Sriram Avatar asked Nov 30 '25 15:11

Sriram


2 Answers

You can't access the resultset soon after you close the connection to the database. Because, when you use resultset.next() method, it will send a request to the database in order to fetch the next record; but physically the connection has been closed. So, an Exception will be thrown by your statement resultset.next().

like image 85
Sankar Avatar answered Dec 03 '25 05:12

Sankar


Once the Connection is closed other resources (Statements or ResultSets) must not be used.

like image 40
Azodious Avatar answered Dec 03 '25 06:12

Azodious



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!