Javadoc says that for .close() PreparedStatementsaying he ..
.close()
PreparedStatement
Immediately frees up the Statement object database and JDBC resources instead of waiting for it to happen when it is automatically closed. Generally, it is good practice to release resources as soon as you are done with them to avoid linking database resources.Calling the method of closing the Statement object that is already closed does not affect.Note. When a Statement object is closed, its current ResultSet, if it exists, is also closed.
Immediately frees up the Statement object database and JDBC resources instead of waiting for it to happen when it is automatically closed. Generally, it is good practice to release resources as soon as you are done with them to avoid linking database resources.
Calling the method of closing the Statement object that is already closed does not affect.
Note. When a Statement object is closed, its current ResultSet, if it exists, is also closed.
Consider the following scenario
MyConnector databaseConnector = DBManager.instance().getConnector(); Connection con = databaseConnector.getConnection(); // returns java.sql.Connection PreparedStatement pstmt = null; try { pstmt = con.prepareStatement("some query"); ... } finally { if (pstmt != null) pstmt.close(); }
In this example will be pstmt.close()closed con?
pstmt.close()
con
a Statement Connection. a Connection Statement.
Statement
Connection
:
, a Connection Statement ResultSet, .
ResultSet
, - (ResultSet, Statement, Connection), .
. Statement , ResultSet [ ], , .
, resultset.
Java 7 try-with-resources, Statement ResultSet.
try-with-resources
try (Connection conn = databaseConnector.getConnection(); PreparedStatement pstmt = conn.prepareStatement("some query")) { ... } catch (Exception e) { ... }