*How does ResultSet work?
*What is Fetch size?
Does ResultSet get all data from db only at once?
There exists Statement.setMaxRows() which does the same on the Statement Interface.
You can then use ORDER BY DESC/ASC and get the results you need.
ResultSet provides an interface that the database driver implements to pass you the results of a query. Fetch size is the number of rows the driver gets in each trip to the database. A ResultSet gets all the data at once if the fetch size is larger than the number of rows. Otherwise, it gets it in chunks.
About the Fetch Size Attribute
Each ResultSet object has a fetch size attribute. This attribute controls how many rows the driver should retrieve from the server in each server request.
If fetch size is 1, the driver will get one row from the server with each ResultSet.next() method call.
If the fetch size is 20, the driver will get twenty rows from the server on the first ResultSet.next(). All 20 rows will be kept in the driver network buffer. The subsequent 19 ResultSet.next() calls will not talk to the server at all. On the 20th ResultSet.next() the server will be asked for the next 20 rows.
The driver is not required to use the fetch size strictly. The driver should use the fetch size only as a recommendation from the application. However, Mimer JDBC does follow a specified fetch size.
When the ResultSet object is created, it inherits the fetch size from the statement object (Statement, PreparedStatement or CallableStatement) that created the ResultSet. The fetch size attribute for the statement object can be manipulated just like the ResultSet attribute but its sole purpose is to provide a default for ResultSet objects.
If the application does not specify a fetch size at all, Mimer JDBC will use whatever fetch size that fits into a 60Kb buffer. If each row takes up 100 bytes the default fetch size will be 600.
How do I set the fetch size?
There are two ways you can set the fetch size:
Set the fetch size on the statement object, thus affecting all ResultSet objects created by the statement object
Set the fetch size individually on each ResultSet object created.
For example, the code sample below creates a PreparedStatement and sets the default fetch size to 1000 rows. This setting could be suitable for a non-interactive caller. Sometimes an interactive part of the application wants to present the same information to the user one screen at a time. In this case, a result set is created from the PreparedStatement and the fetch size is set to 20.
[ August 10, 2005: Message edited by: Yilmaz Mete ]