Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closing ResultSet in Java 7 [duplicate]

Tags:

java

jdbc

The following site shows using the new "AutoClosable" features with JDBC: link. This site is showing how the Statement will be automatically closed, but the result set is not in the try() section where it would be auto-closed. So, my question is, do I NOT need to close ResultSets directly in Java 7? I have always used the pattern: close resultset, close statement, close connection.

like image 362
Rocky Pulley Avatar asked Jul 12 '12 15:07

Rocky Pulley


People also ask

Can we close ResultSet in Java?

To close a Statement , ResultSet , or Connection object that is not declared in a try -with-resources statement, use its close method. If auto-commit is disabled, you must explicitly commit or roll back active transactions before you close the connection.

Does closing ResultSet close Statement?

closing the Statement automatically closes any open ResultSet created from it; executing SQL on a Statement also closes any previously-open ResultSet on that statement.

Should I close ResultSet and PreparedStatement?

You should explicitly close your Statement and PreparedStatement objects to be sure. ResultSet objects might also be an issue, but as they are guaranteed to be closed when the corresponding Statement/PreparedStatement object is closed, you can usually disregard it.

Why is ResultSet closed?

A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results. The number, types and properties of a ResultSet object's columns are provided by the ResultSetMetaData object returned by the ResultSet.


1 Answers

From the Javadoc of ResultSet:

A ResultSet object is automatically closed when the Statement object that generated it is closed, re-executed, or used to retrieve the next result from a sequence of multiple results.

like image 119
Durandal Avatar answered Oct 14 '22 00:10

Durandal