比较 jdbc 中的结果集
在我的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此代码检查两个结果集的所有列,并且它们的行数必须相等。
This code checks all the columns of two resultsets, and also the number of rows must be equal in them.
使用 JDBC,您必须迭代两个 ResultSet 对象并比较它们中的每个字段。
如果你可以用 SQL 做到这一点,那么我会尝试
,并且
两者都应该返回空结果
如果使用库是你的一个选择,你可以尝试 jOOQ(我在 jOOQ 背后的公司工作)。 jOOQ 围绕 JDBC 封装了许多有用的功能。使用 jOOQ,您可以运行
或者也可以:
然后
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
and
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
or also:
And then
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().
作为 rs1 & rs2 是不同的对象,它们指向不同的内存位置,因此比较 rs1 和 rs2 rs2 不正确,而是使用下面的代码:-
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 :-