I want to merge two Resultset Ob开发者_如何学Goject into third one.
If you have any idea then reply..
vipul
You can't merge ResultSet
s directly. You have two options:
Create a new class that effectively combines the results; or
If the queries have the same type and number of columns and come from the same database you can
UNION
or (preferably)UNION ALL
them so they come back in oneResultSet
.
To clarify (1) a ResultSet
is effectively a cursor. A certain number of results will be loaded but the rest won't be until you progress to them. How do you want to combine them? One after the other? Sorted in some way? Basically you'd have to do that yourself but looping through multiple ResultSet
s is a small piece of code.
ResultSet is an interface, so you are free to implement it however you want. In your case, you could create a class that implemented ResultSet, but had a constructor that took two other ResultSets. Then implement each method to delegate to either the first ResultSet or the second. For example, the first() method would call rs1.first(), while last() would call rs2.last(). With next(), you'd have to be clever and call rs1.next() until rs1 ran out of rows, then switch to rs2. You'd have to keep track which of the two ResultSets was "current" for the next() calls.
精彩评论