查询返回多个结果集 [英] Queries returning multiple result sets

查看:117
本文介绍了查询返回多个结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MSSQL 数据库并运行以下查询:

I have a MSSQL database and am running the following query:

select * from projects; select * from user

上面的查询一次返回两个结果集,我无法单独触发两个查询。如何在Java类中同时处理结果集?

The above query returns two result sets at once, and I cannot fire both queries separately. How can I handle both the result set at once in a Java class?

推荐答案

正确处理多个 JDBC语句返回的ResultSet :

PreparedStatement stmt = ...;
boolean isResultSet = stmt.execute();

int count = 0;
while(true) {
    if(isResultSet) {
        rs = stmt.getResultSet();
        while(rs.next()) {
            processEachRow(rs);
        }

        rs.close();
    } else {
        if(stmt.getUpdateCount() == -1) {
            break;
        }

        log.info("Result {} is just a count: {}", count, stmt.getUpdateCount());
    }

    count ++;
    isResultSet = stmt.getMoreResults();
}

重要位:


  • getMoreResults() execute() return false 表示语句的结果只是一个数字,而不是 ResultSet

  • 你需要检查 stmt.getUpdateCount()== -1 以了解是否有更多结果。

  • 确保关闭结果设置或使用 stmt.getMoreResults(Statement.CLOSE_CURRENT_RESULT)

  • getMoreResults() and execute() return false to indicate that the result of the statement is just a number and not a ResultSet.
  • You need to check stmt.getUpdateCount() == -1 to know if there are more results.
  • Make sure you either close the result sets or use stmt.getMoreResults(Statement.CLOSE_CURRENT_RESULT)

这篇关于查询返回多个结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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