In the tutorial “Using Prepared Statements” it states that they should always be closed. Suppose I have a function
getPrice() {
}
that I expect to be called multiple times per second. Should this method be opening and closing the PreparedStatement with every single method call? This seems like a lot of overhead.
First of all,
PreparedStatementare never opened. It’s just a preparedStatementthat is executed. The statement is sent to the RDBMS that executes the SQL statement compiled by thePreparedStatement. The connection to the SQL statement should be opened during the duration of the SQL querying and closed when no other RDMS calls is needed.You can send many
Statement/PreparedStatementas you require provided that you finally close itsResultSetandPreparedStatementonce you’re completed with them and then close the RDBMS connection.