1

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

4
  • 1
    would you mind posting a little sample of the data and the queries? Commented Mar 7, 2014 at 1:05
  • i just posted some more information on the sets. thank you Commented Mar 7, 2014 at 1:13
  • What are set1 and set2. What data type? Commented Mar 7, 2014 at 1:25
  • set1 and set2 are resultset from sql queries. 'id' is int. 'name' and 'alias' are string Commented Mar 7, 2014 at 1:33

2 Answers 2

1

The below loop will only executed once for the outer loop

        while (set2.next()){
            if (set1.getInt(1)==(set2.getInt(1))){  
                System.out.print(", Alias: "+set2.getString(2);
                break;
            }
        }

You need to reinitialize your iterator each time the outer loop executes.

Sign up to request clarification or add additional context in comments.

3 Comments

sorry, can you elaborate on reinitialize the iterator? I am very new to java
You must have initialized set2 in your code right? something like Iterator<Integer> set2 = yourSet.iterator(). You have to do this before the inner while loop.
i added Iterator<Integer> set2_itr = set2.iterator(). It said there is no iterator() method. I did import java.util.* though
0

Since the two result sets have the same first column (id), you can start from the first row for both result sets. In the inner loop, iterate over the rows in set2 until its id is great than the id in set1. So you can do:

while (set1.next())
{
    System.out.print("Id: "+set1.getInt(1)+", Name: "+set1.getString(2));

    while (set2.next())
    {
        System.out.print(", Alias:");
        if (set1.getInt(1)==(set2.getInt(1)))
        {  
            System.out.print(set2.getString(2) + ", ");
            continue;
        } else {
            set2.previous();
            break;
        }
    }
    System.out.println();
}

3 Comments

that's a good idea. but i cannot do that becoz the id in set2 is not unique. so one 'Name' could have multiple 'Alias'
thank you. it actually says the operation is not supported on forward only result set. Is there a better way to merge results from the 2 result sets? I think ppl often use UNION in sql query. But I cannot use UNION becoz the returned rows have different fields ('Name' and 'Alias')
How did you obtain the two ResultSet objects? ResultSet class allows you to move cursor back to one row using previous() method. What DB are you using? But if you want to use SQL directly, I think you can use the query: SELECT table1.id, table1.name, table2.alias FROM table1 INNER JOIN ON table1.id=table2.id ORDER BY table1.id. Not sure if this works but you can try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.