misguided
misguided

Reputation: 3799

Comparing Resultsets In java

I am looking for ways to compare two ResultSets in java (RS1 and RS2).

Things to note/Requirements

Example:

RS1                        RS2
Column1 Column2            Column1 Column2 Column3
A       1                  A       2       ITEM1
C       1                  B       4       ITEM2
D       2                  C       2       ITEM3
E       5                  D       1       ITEM4

Expected Result

==> A is valid , because it is in RS1 & RS2, also Column2 for A in RS2 > Column2 for A in RS1

==> B is invalid, as it is not there in RS1

==> C is valid , because it is in RS1 & RS2, also Column2 for C in RS2 > Column2 for C in RS1

==> D is invalid , because even though it is there in RS1 and RS2,the Column2 for D in RS2 < Column2 for D in RS1

==> E is invalid because it is not there in RS2

Looking for possible options to solve this issue.

Upvotes: 2

Views: 2080

Answers (4)

Joe2013
Joe2013

Reputation: 997

Loop through the first ResultSet and Store them in a HashMap<String , Object> where use column1 as the Key and the put all the remaining columns in a Bean object.

After that iterare through the second resultSet and check whether column1 exists in the HashMap or not by just doing a lookup. if it exists, then get the Object using getValue and then compare with the resultSet2 columns.

Upvotes: 0

Yair Zaslavsky
Yair Zaslavsky

Reputation: 4137

I would suggest to read the values from each ResultSet, and put it in List and then compare the two Lists.
You can perform getMetaData on each resultset in order to get information on the columns.
This will include information like the number of the columns, and then you will be able to write a simple loop that will get the value of each column and add it into the list.

Upvotes: 0

Rahul
Rahul

Reputation: 45080

Traverse through both your ResultSets and store them in a Map<String, Integer>. After that you can traverse through either of the keySet and keep comparing the values.

This lets you compare 2 ResultSets from any kind of DB(Oracle, MySql, SQLServer, etc.).

Upvotes: 3

Vikdor
Vikdor

Reputation: 24134

I would simply read the results into two Maps with key as the primary key which seems to be Column1 in your case, the value is Column2.

Then you can iterate through the entry set of the first map and see if the second map has the key and if so, compare the values.

Upvotes: 0

Related Questions