gpt4 book ai didi

java - 比较 jdbc 中的结果集

转载 作者:搜寻专家 更新时间:2023-10-31 19:30:46 24 4
gpt4 key购买 nike

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

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

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

谢谢

最佳答案

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

    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++;
}

关于java - 比较 jdbc 中的结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6582093/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com