I have a cursor returned from Database executes in 31ms (milliseconds) .
But when I use this cursor having more than 1500 rows for fetching rows
ResultSet rs = (ResultSet)cstm.getObject(6);
while(rs.next()){
system.out.println("...");
}
Just simple transversing through each row of cursor it's taking more than 40 seconds (40000 ms)
What can be done?
As to the concrete question about rs. next() , it shifts the cursor to the next row of the result set from the database and returns true if there is any row, otherwise false .
If there are no rows next to its current position this method returns false, else it returns true. Using this method in the while loop you can iterate the contents of the result set. while(rs.next()){ System.out.println(rs.getString(1)); System.out.println(rs.getString(2)); }
The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. A default ResultSet object is not updatable and has a cursor that moves forward only.
Moves the cursor forward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.
Indeed, by default JDBC use a fetch size of 10.
Thus, if you don't set a greater value, you'll call database for next records exactly 150 times ..., no need to explain drawbacks of round-trips.
All you have to do is to test performance by setting fetchSize
to.. 100 for instance :
statement.setFetchSize(100);
You can play with this number to improve performance according to your environnement.
you have more than 1500 rows in your cursor and the rs what is returned by the database is just a reference to that cursor. So when you invoke rs.next(), every time it goes to the database cursr and get you the current record pointed by the cursor pointer.
So obviously it will take some time to go to database every time and fetch a single record for more than 1500 times with every while loop iteration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With