The relationship between "close" for PreparedStatement and Connection?

Javadoc says that for .close() PreparedStatementsaying he ..

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?

+5
source share
3 answers

a Statement Connection. a Connection Statement.

:

  • → → Statement
  • Statement → → ResultSet

, a Connection Statement ResultSet, .

, - (ResultSet, Statement, Connection), .

+9

. Statement , ResultSet [ ], , .

, resultset.

+2

Java 7 try-with-resources, Statement ResultSet.

try (Connection conn = databaseConnector.getConnection();
     PreparedStatement pstmt = conn.prepareStatement("some query")) {
    ...
} catch (Exception e) {
    ...
}
0
source

All Articles