如何在Java中合并两个ResultSet? [英] How to combine two ResultSets in Java?
问题描述
我有两个具有相同字段的结果集(rs1
和rs2
).现在,如何将这两个结果集合并为一个,以使重复的行显示一次.
I have two result sets (rs1
and rs2
) having same fields. Now how to combine those two result sets into one so that duplicate rows are shown once.
推荐答案
如果两个ResultSet
来自同一数据库,那么为什么在使用union
进行检索时不将它们组合在一起;例如
if the two ResultSet
s are from the same database then why not combine them during the retrieval by using union
; e.g.
select A, B
from C
union
select A, B
from D
但是,如果这不是一个选项,那么我建议定义一个Row
类以表示从ResultSet
中提取的行,并实现equals
/hashCode
以允许比较Row
是否相等.然后只需将每个Row
添加到Set
(例如HashSet
)以删除重复项.
However, if this is not an option then I suggest defining a Row
class to represent a Row extracted from your ResultSet
and implementing equals
/ hashCode
to allow the Row
to be compared for equality. Then simply add each Row
to a Set
(e.g. HashSet
) in order to remove duplicates.
这篇关于如何在Java中合并两个ResultSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!