I want to merge results from 2 resultSet which are results returned from sql queries. What is the best way to accomplish this?
My plan is lopping over 2 result sets to print out the final result from both result sets. Both results set have the same id column which are their first column. But for the inner while loop, I only get the first value printed out.
I thought when there is a match, it prints the value from the second result set, and break out of the loop. Then when there is another match, it prints out the value again.
while (set1.next())
{
System.out.print("Id: "+set1.getInt(1)+", Name: "+set1.getString(2));
while (set2.next())
{
if (set1.getInt(1)==(set2.getInt(1)))
{
System.out.print(", Alias: "+set2.getString(2);
break;
}
}
System.out.println();
}
Edit:
ResultSet set1:
id | name
1 | A
2 | B
3 | C
...
ResultSet set2:
id | alias
1 | F
2 | G
2 | H
I want to print out:
Id: 1, Name: A, Alias: F
Id: 2, Name: B, Alias: G, H
FYI, the id is in ascending orders in both sets
set1andset2. What data type?