java 比较 jdbc 中的结果集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6582093/
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
Comparing resultsets in jdbc
提问by Jim
In my java code i have two resultsets rs1 and rs2 obtained as follows :
在我的 Java 代码中,我获得了两个结果集 rs1 和 rs2,如下所示:
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.
这两个表具有相同的架构,由字段 ID、名称和地址组成,我想比较两个结果集。我可以直接做 rs1 == rs2 吗?如果没有,我应该如何比较两个结果集?一些例子将不胜感激。
Thank You
谢谢
回答by Lukas Eder
With JDBC, you will have to iterate over both ResultSet
objects and compare every field in them.
使用 JDBC,您必须遍历两个ResultSet
对象并比较它们中的每个字段。
If you can do it with SQL, then I'd try
如果你可以用 SQL 做到这一点,那么我会尝试
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
如果您可以选择使用库,您可以尝试jOOQ(我为 jOOQ 背后的公司工作)。jOOQ 围绕 JDBC 包装了许多有用的特性。使用 jOOQ,您可以运行
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
}
回答by gyorgyabraham
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++;
}
回答by Xote
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.
rs1 和 rs2 不会是相同的对象,因此比较它们是毫无意义的。我猜您想比较结果集的内容。
You may get the table scheme from the metadata by rs1.getMetaData().
您可以通过 rs1.getMetaData() 从元数据中获取表方案。
回答by Gagan
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 :-
由于 rs1 和 rs2 是不同的对象,它们指向不同的内存位置,因此比较 rs1 和 rs2 是不合适的,而是使用下面的代码:-
while (rs.next()&& rs1.next())
{
int userId = rs.getInt("userId")
int userId1 = rs1.getInt("userId")
if(userId == userId1){
// do some thing
}
}