When does an SQL Statement object automatically close?

My question is regarding this statement:

Immediately frees up the Statement object database and JDBC resources instead of waiting for it to happen when it is automatically closed.    http://docs.oracle.com/javase/6/docs/api/java/sql/Statement.html#close%28%29

So, in what cases is the Statement object automatically closed?

+3
source share
4 answers

For JDBC 4.0 and earlier (Java 6 and earlier):

  • Application closes when connection is closed

JDBC 4.1 (Java 7) adds the following:

  • , closeOnCompletion() ResultSet ( closeOnCompletion)

, "try-with-resources" , "" try ... finally.

+4

, , , .

+1

try-with-resources java.sql.Connection, java.sql.Statement java.sql.ResultSet , , SQLException - . try-with-resources ( ). : try-with-resources JDBC

try-with-resources, stmt, , try :

try (Statement stmt = con.createStatement()) {

  // ...

}
+1

If you are concerned that you are sure to close connections, resources, etc., I would just use spring jdbc. It adds a bit of overhead, it's pretty easy to implement, and will force you to get rid of these tedious try-catch-finally structures just for simple queries.

+1
source

All Articles