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?
For JDBC 4.0 and earlier (Java 6 and earlier):
JDBC 4.1 (Java 7) adds the following:
closeOnCompletion()
ResultSet
, "try-with-resources" , "" try ... finally.
try ... finally
, , , .
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()) { // ... }
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.