开发者

java problem about ResultSet

I want to merge two Resultset Ob开发者_如何学Goject into third one.

If you have any idea then reply..

vipul


You can't merge ResultSets directly. You have two options:

  1. Create a new class that effectively combines the results; or

  2. 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 one ResultSet.

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 ResultSets 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜