在 Java 中将多个 ResultSet 合并为单个 ResultSet

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16436806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:56:06  来源:igfitidea点击:

Merging Multiple ResultSet into Single ResultSet in Java

javadatabasejdbcresultset

提问by Priyank Doshi

Let say I have multiple ResultSet(each resultSet would refer to 1 row in database) (they are of same table.) . Now I want to create consolidated ResultSet which would intern have all other resultSet. So my primary goal is to create a combined ResultSet which would point to all rows which where previously pointed by individual resultSet.

假设我有多个 ResultSet(每个 resultSet 将引用数据库中的 1 行)(它们属于同一个表。)。现在我想创建合并的 ResultSet,它将实习生拥有所有其他 resultSet。所以我的主要目标是创建一个组合的 ResultSet,它将指向以前由单个 resultSet 指向的所有行。

I am using Java. Do any one know how pragmatically we can achieve this?

我正在使用 Java。有谁知道我们可以如何务实地实现这一目标?

Edit : we are using java.sql.ResultSet.

编辑:我们正在使用java.sql.ResultSet.

Edit : To make it more clear :

编辑:为了更清楚:

Let say I have

假设我有

List<ResultSet> someResults ; // each resultSet Would point to a single row in database.

I want to create a consolidated ResultSet finalResults;

我想创建一个统一的 ResultSet finalResults;

psudo code :

伪代码:

List<ResultSet> resultSets = // poppulated from code
ResultSet rs  = convert(resultSets) // psude conver method

回答by Thihara

If you are talking about a java.sql.ResultSet, it's not possible as far as I know. I suggest you change your query instead.

如果您在谈论 java.sql.ResultSet,据我所知这是不可能的。我建议您改为更改查询。

Or you can retrieve the results into an java object and then combine all the objects into a Collection.

或者您可以将结果检索到一个 java 对象中,然后将所有对象组合到一个集合中。

回答by SudoRahul

If the results are gonna be from the same table, why not use UNION/UNION ALL(depending on your needs) in your query itself.

如果结果将来自同一个表,为什么不在UNION/UNION ALL查询本身中使用(取决于您的需要)。

Something like this:

像这样的东西:

select A, B
from C where Q = R
union
select A, B
from C where P = S

Or else, there is the hard way of iterating through each result set and populating POJOs and adding them to a List/Set(Based on your needs). This seems to be an overkill if there are a lot of result sets.

否则,很难遍历每个结果集并填充 POJO 并将它们添加到List/Set(根据您的需要)。如果有很多结果集,这似乎是一种矫枉过正。

回答by Evgeniy Dorofeev

I would do it this way

我会这样做

class GatheringResultSet implements ResultSet {
    List<E> resultSets;
    ResultSet current;

    GatheringResultSet(List resultSets) {
        this.resultSets = new ArrayList(resultSets);
        current = resultSets.remove(0);
    }

    @Override
    public boolean next() throws SQLException {
        if (current.next()) {
            return true;
        }
        if (resultSets.isEmpty()) {
            return false;
        }
        current = resultSets.remove(0);
        return true;
    }

...

...

the rest of the methods just delegate call to current ResultSet

其余的方法只是将调用委托给当前的 ResultSet

回答by Ramkumar Pillai

public class ResultSets {
        private java.util.List<java.sql.ResultSet> resultSets;

        private java.sql.ResultSet current;

        @lombok.SneakyThrows
        public ResultSets(java.util.List<java.sql.ResultSet> resultSets) {
            this.resultSets = new java.util.ArrayList<>(resultSets);
            current = resultSets.remove(0);
        }
        @lombok.SneakyThrows
        public boolean next() {
            if (current.next()) {
                return true;
            }else if (!resultSets.isEmpty()) {
                current = resultSets.remove(0);
                return next();
            }
            return false;
        }

        @lombok.SneakyThrows
        public int getInt(int pos){
            return current.getInt(pos);
        }

        @lombok.SneakyThrows
        public String getString(String field){
            return current.getString(field);
        }
    }

Usage :-

用法 :-

  @lombok.SneakyThrows
  public static void main(String ... args) {

        Connection conn =  pe.getConnection("backup");
        String sql1 = "SELECT count(distinct(User_Key)) FROM user_table";
        String sql2 = "SELECT count(distinct(Username)) FROM user_table";

        Statement stmt1 = conn.createStatement();
        Statement stmt2 = conn.createStatement();

        List<ResultSet> resultSets = new ArrayList<>();
        resultSets.add(stmt1.executeQuery(sql1));
        resultSets.add(stmt2.executeQuery(sql2));

        ResultSets rs = new ResultSets(resultSets);

        while(rs.next()){
           System.out.println(rs.getInt(1));

        }
  }