Wednesday 1 July 2020

How to iterate ResultSet multiple times in java

How to iterate ResultSet multiple times


Can someone tell me, how can I iterate resultset more than one time. As I want to use same resultset multiple times, to avoid database trip for same query.
I observed that I cannot iterate result set more than once, it sets it as null and throws null pointer exception. I am using following code

?
1
2
3
4
5
6
7
8
9
10

Code Statements of  iterate ResultSet

Statement s = c.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = s.executeQuery(sql);
while (rs.next()) {
//Some operation
}
rs.first(); // Here it throws nullpointer exception.  
while (rs.next()) {
//Some operation
}

0 comments:

Post a Comment