This post is about how to implement pagination when you have a very large number of records and you need to retrieve small chunks of records to be displayed on each page from the database at a time.
note: this is a pure "from the scratch" approach and should be treated as the last resort i.e if nothing else works
my implementation was to display 50 records in each page(DB2 database in the backend) and the user should be able to navigate back and forth using "Next" and "Previous" buttons
Implementing "Next" button:
---------------------------------
This is the easier part of the implementation, assume you have a table with 2 columns Emp ID and Emp Name and Emp ID is the key, lets say you retrieved the first 50 records from the database in descending order(showing the latest employees first) on the first page , so if i have 500 employees in all, this will fetch records with Emp ID through 500-450, when the user clicks on "Next" you need to show records from 449-400 in the next page, all you have to do is get hold of the last record in the previous list and run a query, which goes something like this
select Emp ID,Emp Name
from employee
where Emp ID < 450
order by DESC
fetch first 50 records only
this can go on and on till you reach the last page ,
Implementing "Prev" button:
--------------------------------
This is a little tricky , lets suppose i am on page#3 viewing records from 399-350 ,if i want to go to page#2 which is records 449-400 , i need to get hold of the first record in the previous page and run a query like this
select Emp ID,Emp Name
from employee
where Emp ID > 399
order by ASC
fetch first 50 records only
here if you noticed i switched to "order by ASC" , this is the important because if i didnt order the records in ASC , this query would have fetched the first fifty records in the table i.e 500-450 , so by sorting in the reverse order and fetching the first 50 records i was able to get the records 449-400
however this implementation has a problem , this query will return the records in the order 400-449 , but in my pages i need all records to be displayed in the descending order , so you will have to reverse the order of the items fetched from the database in your
java code, which can be done using Collections.sort() and Comparator , below is snippet of the code i used
Collections.sort(lstItems, Collections.reverseOrder(<Object>.<Name of the comparator>));
Agreed that this implementation will create a lot of database calls , but this works prefectly if you cannot fetch say 10K records at one go and handle the pagination on java side using lists etc