查询返回多个结果集

时间:2012-03-14 05:46:27

标签: java sql java-ee jdbc

我有一个MSSQL数据库,我正在运行以下查询:

select * from projects; select * from user

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

7 个答案:

答案 0 :(得分:23)

更正用于处理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()返回false表示该声明的结果只是一个数字,而不是ResultSet
  • 您需要检查stmt.getUpdateCount() == -1以了解是否有更多结果。
  • 确保您关闭结果集或使用stmt.getMoreResults(Statement.CLOSE_CURRENT_RESULT)

答案 1 :(得分:12)

您可以使用Statement.execute(),getResultSet();

PreparedStatement stmt = ... prepare your statement result
boolean hasResults = stmt.execute();
while (hasResults) {
    ResultSet rs = stmt.getResultSet();
    ... your code parsing the results ...
    hasResults = stmt.getMoreResults();
}

答案 2 :(得分:3)

是的,你可以。请参阅此MSDN文章 https://msdn.microsoft.com/en-us/library/ms378758(v=sql.110).aspx

public static void executeStatement(Connection con) {
   try {
      String SQL = "SELECT TOP 10 * FROM Person.Contact; " +
                   "SELECT TOP 20 * FROM Person.Contact";
      Statement stmt = con.createStatement();
      boolean results = stmt.execute(SQL);
      int rsCount = 0;

      //Loop through the available result sets.
     do {
        if(results) {
           ResultSet rs = stmt.getResultSet();
           rsCount++;

           //Show data from the result set.
           System.out.println("RESULT SET #" + rsCount);
           while (rs.next()) {
              System.out.println(rs.getString("LastName") + ", " + rs.getString("FirstName"));
           }
           rs.close();
        }
        System.out.println();
        results = stmt.getMoreResults();
        } while(results);
      stmt.close();
      }
   catch (Exception e) {
      e.printStackTrace();
   }
}

我已经测试过了,它运行正常。

答案 3 :(得分:0)

public static void executeProcedure(Connection con) {
   try {
      CallableStatement stmt = con.prepareCall(...);
      .....  //Set call parameters, if you have IN,OUT, or IN/OUT parameters

      boolean results = stmt.execute();
      int rsCount = 0;

      //Loop through the available result sets.
     while (results) {
           ResultSet rs = stmt.getResultSet();
           //Retrieve data from the result set.
           while (rs.next()) {
        ....// using rs.getxxx() method to retieve data
           }
           rs.close();

        //Check for next result set
        results = stmt.getMoreResults();
      } 
      stmt.close();
   }
   catch (Exception e) {
      e.printStackTrace();
   }
}

答案 4 :(得分:0)

在使用java之前,您需要查看RESULT SETS子句。

MSSQL具有此功能,可以更实用的方式帮助您使用Java代码。

此示例将执行两个查询:

EXEC('SELECT id_person, name, age FROM dbo.PERSON; SELECT id_url, url FROM dbo.URL;')
WITH RESULT SETS
(
  (
    id_person BIGINT,
    name VARCHAR(255),
    age TINYINT
  ),
  (
    id_url BIGINT,
    url VARCHAR(2000)
  )
);

您也可以将存储过程与RESULT SETS一起使用。

更多关于:https://technet.microsoft.com/en-us/library/ms188332(v=sql.110).aspx

答案 5 :(得分:-4)

UNION ALL查询允许您组合2个或更多“选择”查询的结果集。它返回所有行(即使该行存在于多个“select”语句中)。

UNION ALL查询中的每个SQL语句必须在具有相似数据类型的结果集中具有相同数量的字段.........

select * from projects
UNION ALL
select * from user

答案 6 :(得分:-23)

答案: NOT 可能。唯一的方法:将它们作为单独的查询运行。