Using a ResultSet after executing two different queries with statement.executeQuery() [duplicate]

纵饮孤独 提交于 2021-02-19 07:57:05

问题


Given the code below:

    //connection stuff
    ResultSet rs = statement.executeQuery(query1);

    statement.executeQuery(query2);

    while(rs.next){
       //code
    }

Is the result set rs still valid even though a second statement has been executed?

I know that when you close a statement the result set isn't valid any longer, but here the code is simply executing another query and not storing it in a result set.


回答1:


Presuming statement is a Statement, from the javadoc:

By default, only one ResultSet object per Statement object can be open at the same time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All execution methods in the Statement interface implicitly close a statment's current ResultSet object if an open one exists.

The posted code is unsafe - the second call to executeQuery will return a new ResultSet, and given only one can be open at a time rs will not be valid.



来源:https://stackoverflow.com/questions/30766375/using-a-resultset-after-executing-two-different-queries-with-statement-executequ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!