Execute multiple queries using a single JDBC Statement object

前端 未结 5 2064
说谎
说谎 2021-01-31 17:41

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

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-31 18:09

    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.

提交回复
热议问题