tags:

views:

60

answers:

1

Hello All

I am getting lot of records in my Result Set. My concerns are :

  1. How Result Set handle these records internally? and
  2. How a programmer can handle those records in batches So that memory problem would not occur.?

waiting for your answers .. Many Thanks

+2  A: 

You can use Statement.setFetchSize() to control the number of records fetched into memory when you iterate through result set. But in general I think it depends on JDBC driver on how it handles that setting and how it stores fetched records in memory.

EDIT: Here is an example (in theory result set will keep only 1 records in memory):

Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.FETCH_FORWARD);
stmt.setFetchSize(1);
ResultSet srs = stmt.executeQuery("SELECT COF_NAME, PRICE FROM COFFEES");
while (srs.next()) {
   String name = srs.getString("COF_NAME");
   float price = srs.getFloat("PRICE");
   // Do your processing...
}
Superfilin
Thanks : Can you please elaborate this. Means How can I use this?
Look at the example, it's very basic JDBC.
Superfilin
The exact coding depends on the DB and the JDBC driver used. So does your code example *not* work on for example MySQL and PostgreSQL. Ashwani66476 has to elaborate more about the environment. E.g. which DB and which JDBC driver.
BalusC
@BalusC: I agree that it depends on driver, that's why I said in my answer: " it depends on JDBC driver on how it handles that setting ", but in general most of the drivers follow good design pattern and try to honor fetch size setting and do not keep whole result set in memory anyway.
Superfilin
Please note, that ResultSet.TYPE_FORWARD_ONLY and ResultSet.FETCH_FORWARD are very important as they only allow to read your result set once and from first record till last one. With other result set types logic may be different.
Superfilin