Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine two ResultSets in Java?

Tags:

java

jdbc

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.

like image 668
Yatendra Avatar asked Feb 18 '10 16:02

Yatendra


1 Answers

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

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.

like image 63
Adamski Avatar answered Oct 29 '22 00:10

Adamski