使用statement.executeQuery()执行两个不同的查询后使用ResultSet [英] Using a ResultSet after executing two different queries with statement.executeQuery()

查看:328
本文介绍了使用statement.executeQuery()执行两个不同的查询后使用ResultSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码:

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

    statement.executeQuery(query2);

    while(rs.next){
       //code
    }

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

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.

推荐答案

假定statementStatement,来自

默认情况下,每个Statement对象只能同时打开一个ResultSet对象.因此,如果一个ResultSet对象的读取与另一个的读取交错,则每个都必须由不同的Statement对象生成.如果存在打开的语句,Statement接口中的所有执行方法都将隐式关闭该语句的当前ResultSet对象.

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的第二次调用将返回一个新的ResultSet,并且一次只能打开一个rs无效.

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.

这篇关于使用statement.executeQuery()执行两个不同的查询后使用ResultSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆