Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to close 'PreparedStatement'? [closed]

Tags:

java

jdbc

I have a website which is getting a massive number of hits. I'm experienced problems, including JDBC connection errors.

I'm a bit confused about closing PreparedStatement. Do I need to close PreparedStatement or is it just enough to only close Statement?

Also, what about ResultSet? Do I need to close it too?

like image 644
user836026 Avatar asked Jan 27 '13 10:01

user836026


People also ask

Does closing connection close PreparedStatement?

Resultset, preparedstatement, statement all are created from connection. If you have closed the connection, automatically all above will be closed as parent is not available.

Do we need to close statement in JDBC?

JDBC Statement objects must always be closed by the application instead of allowing them to be closed by garbage collection.

Should you close ResultSet?

You should explicitly close Statements , ResultSets , and Connections when you no longer need them, unless you declare them in a try -with-resources statement (available in JDK 7 and after). Connections to Derby are resources external to an application, and the garbage collector will not close them automatically.

Does closing a statement close the ResultSet?

(2) Closing the statement object does not close the resultset object.


1 Answers

Yes, you have to close the prepared statements (PreparedStatement Object) and result sets as they may cause memory leakage.

For more information, see Using Prepared Statements.

like image 121
prasanth Avatar answered Sep 29 '22 13:09

prasanth