比较jdbc中的结果集 [英] Comparing resultsets in jdbc

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

问题描述

在我的Java代码中,我有两个结果集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")

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

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.

谢谢

推荐答案

使用JDBC,您将不得不遍历两个ResultSet对象并比较它们中的每个字段.

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

如果您可以使用SQL进行操作,那么我会尝试

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

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,您可以运行

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");

或:

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

然后

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

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

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