比较 jdbc 中的结果集

发布于 2024-11-19 00:53:23 字数 278 浏览 3 评论 0原文

在我的java代码中,我获得了两个结果集rs1和rs2,如下所示:

rs1 = statement.executeQuery("select * from tableA")
rs2 = statement.executeQuery("select * from tableB")

这两个表具有相同的架构,由字段ID、名称和地址组成,我想比较这两个结果集。我可以直接做 rs1 == rs2 吗?如果不是,我应该如何比较两个结果集? 一些例子将非常感激。

谢谢

In my java code i have two resultsets rs1 and rs2 obtained as follows :

rs1 = statement.executeQuery("select * from tableA")
rs2 = statement.executeQuery("select * from tableB")

Both the tables have the same schema consisting of field ID,Name and Address and i want to compare the two resultsets. Can i directly do rs1 == rs2 ?. If no how should i go about comparing the two resultsets ?.
Some example would be really appreciated.

Thank You

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻之岛 2024-11-26 00:53:23

此代码检查两个结果集的所有列,并且它们的行数必须相等。

    int col = 1;
    while (rs1.next() && rs2.next()) {
        final Object res1 = rs1.getObject(col);
        final Object res2 = rs2.getObject(col);
        // Check values
        if (!res1.equals(res2)) {
            throw new RuntimeException(String.format("%s and %s aren't equal at common position %d",
                res1, res2, col));
        }

        // rs1 and rs2 must reach last row in the same iteration
        if ((rs1.isLast() != rs2.isLast())) {
            throw new RuntimeException("The two ResultSets contains different number of columns!");
        }

        col++;
    }

This code checks all the columns of two resultsets, and also the number of rows must be equal in them.

    int col = 1;
    while (rs1.next() && rs2.next()) {
        final Object res1 = rs1.getObject(col);
        final Object res2 = rs2.getObject(col);
        // Check values
        if (!res1.equals(res2)) {
            throw new RuntimeException(String.format("%s and %s aren't equal at common position %d",
                res1, res2, col));
        }

        // rs1 and rs2 must reach last row in the same iteration
        if ((rs1.isLast() != rs2.isLast())) {
            throw new RuntimeException("The two ResultSets contains different number of columns!");
        }

        col++;
    }
孤寂小茶 2024-11-26 00:53:23

使用 JDBC,您必须迭代两个 ResultSet 对象并比较它们中的每个字段。

如果你可以用 SQL 做到这一点,那么我会尝试

select * from tableA
except -- or minus in oracle
select * from tableB

,并且

select * from tableB
except -- or minus in oracle
select * from tableA

两者都应该返回空结果

如果使用库是你的一个选择,你可以尝试 jOOQ(我在 jOOQ 背后的公司工作)。 jOOQ 围绕 JDBC 封装了许多有用的功能。使用 jOOQ,您可以运行

Result<Record> r1 = create.fetch("select * from tableA");
Result<Record> r2 = create.fetch("select * from tableB");

或者也可以:

r1 = create.fetch(rs1);
r2 = create.fetch(rs2);

然后

if (r1.equals(r2)) {
    // the results are equal
}
else {
    // the results are not equal
}

With JDBC, you will have to iterate over both ResultSet objects and compare every field in them.

If you can do it with SQL, then I'd try

select * from tableA
except -- or minus in oracle
select * from tableB

and

select * from tableB
except -- or minus in oracle
select * from tableA

Both should return an empty result

If using a library is an option for you, you could try jOOQ (I work for the company behind jOOQ). jOOQ wraps many useful features around JDBC. With jOOQ, you could run

Result<Record> r1 = create.fetch("select * from tableA");
Result<Record> r2 = create.fetch("select * from tableB");

or also:

r1 = create.fetch(rs1);
r2 = create.fetch(rs2);

And then

if (r1.equals(r2)) {
    // the results are equal
}
else {
    // the results are not equal
}
扬花落满肩 2024-11-26 00:53:23

rs1 和 rs2 不会是相同的对象,因此比较它们是毫无意义的。
我猜你想比较结果集的内容。

您可以通过 rs1.getMetaData() 从元数据中获取表方案。

rs1 and rs2 wont be the same objects, so comparing them is rather senseless.
I guess you want to compare the content of the resultsets.

You may get the table scheme from the metadata by rs1.getMetaData().

野生奥特曼 2024-11-26 00:53:23

作为 rs1 & rs2 是不同的对象,它们指向不同的内存位置,因此比较 rs1 和 rs2 rs2 不正确,而是使用下面的代码:-

while (rs.next()&& rs1.next())
{
  int userId = rs.getInt("userId")
  int userId1 = rs1.getInt("userId")
  if(userId == userId1){
   // do some thing
  }

}

As rs1 & rs2 are different objects, they point to different memory location, hence comparison of rs1 & rs2 is not proper, rather you use, the code below :-

while (rs.next()&& rs1.next())
{
  int userId = rs.getInt("userId")
  int userId1 = rs1.getInt("userId")
  if(userId == userId1){
   // do some thing
  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文