tags:

views:

95

answers:

2

I need to fetch many records from an RDBMS in Java (10-20k) my target system expects them to be available as Java List. So I want to implement my code as "Virtual list" where I actually only fetch the records I actually need. I expect SQL like

SELECT * FROM CUSTOMER WHERE COUNTRY="Moldovia"

as parameter and just return what is requested. Most likely the data is requested in batches of 50. Any hints how to do that?

+1  A: 

Use OFFSET and LIMIT in your query:

SELECT * FROM CUSTOMER WHERE COUNTRY="Moldovia" LIMIT 50 OFFSET 50

Assuming of course that your SQL dialect allows it. The example will return rows 51-100.

Vinay Sajip
stwissel
No, that's what you'd need to do.
Vinay Sajip
+2  A: 

Unless you expect your clients to randomly access the data, you're probably better off returning an Iterator. Also, take a look at ResultSet.setFetchSize: http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html#setFetchSize(int)

So something like:

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;

public class FooResultSetIterator implements Iterator<Foo>
{
  private final ResultSet resultSet;
  private boolean hasNext;

  FooResultSetIterator(final ResultSet resultSet, final int fetchSize) throws SQLException
  {
    this.resultSet = resultSet;
    this.resultSet.setFetchSize(fetchSize);
    this.hasNext = resultSet.next();
  }

  @Override
  public boolean hasNext()
  {
    return hasNext;
  }

  @Override
  public Foo next()
  {
    final Foo foo = new Foo(resultSet);
    try
    {
      this.hasNext = resultSet.next();
    }
    catch (final SQLException e)
    {
      throw new RuntimeException(e);
    }
    return foo;
  }

  @Override
  public void remove()
  {
    throw new UnsupportedOperationException("Cannot remove items from a ResultSetIterator");
  }

}

class Foo
{
  public Foo(ResultSet resultSet)
  {
    // TODO Auto-generated constructor stub
  }
}
SimonC
Hi Simon,thx for the answer. I need to do a list. The other system requires that, but of course a list has an iterator. Basically the results will be visualized in a table 50 entries at a time and a user can jump to any page. So I need to get the total size so the paging navigator can be rendered correctly
stwissel