In JDBC, can I use single Statement
object to call executeQuery(\"\")
multiple times? Is it safe? Or should I close the statement object after each que
I can't find anything in the API docs that would state, that you shouldn't call executeQuery()
on a given PreparedStatement
instance more than once.
However your code does not close the PreparedStatement
- a call to executeQuery()
would throw a SQLException
in that case - but the ResultSet
that is returned by executeQuery()
. A ResultSet is automatically closed, when you reexecute a PreparedStatement
. Depending on your circumstances you should close it, when you don't need it anymore. I would close it, because i think it's bad style not to do so.
UPDATE Upps, I missed your comment between the two try blocks. If you close your PreparedStatement at this point, you shouldn't be able to call executeQuery() again without getting a SQLException.