如何在Java中合并两个ResultSet? [英] How to combine two ResultSets in Java?

查看:90
本文介绍了如何在Java中合并两个ResultSet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有相同字段的结果集(rs1rs2).现在,如何将这两个结果集合并为一个,以使重复的行显示一次.

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 ResultSets 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屋!

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