使用 statement.executeQuery() 执行两个不同的查询后使用结果集

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

给定以下代码:

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

    statement.executeQuery(query2);

    while(rs.next){
       //code
    }

即使执行了第二条语句,结果集rs是否仍然有效?

我知道当您关闭语句时结果集不再有效,但这里的代码只是执行另一个查询而不是将其存储在结果集中。

假设 statementStatement,来自 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.

发布的代码不安全 - 第二次调用 executeQuery 将 return 一个新的 ResultSet,并且一次只能打开一个 rs 将无效。