可以参考hibernate的分页实现方法.

解决方案 »

  1.   

    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;/**
     * @author fisher
     * 
     * <pre>
     * 
     * Title: QueryManager.java
     * 
     * Description: Provide with a common pagination
     *
     * </pre>
     */
    public class PaginationManager { // The cache contains records
    // capacity: the capacity of the list
    // startIndex: the current start index in list
    // endIndex: the current end index in list
    // currentIndex: the 
    private List records  = new ArrayList();
    private int  capacity = 1000;
    private int startIndex = 0;
    private int endIndex   = 0;
    private int currentIndex = 0;

    // The hander for pagination
    private Paginable handler = null;

    // The number of records each page contains
    private int pageSize;



    public PaginationManager(Paginable handler) {
    this(handler, 1000, 40);
    }

    public PaginationManager(Paginable handler, int pageSize) {
    this(handler, 1000, pageSize);
    }

    /**
     * Construct a PaginationManager instance
     * 
     * @param handler The handler mapulate pagination
     * @param capacity The capacity of records buffer
     * @param pageSize The size of each page
     */
    public PaginationManager(Paginable handler, int capacity, int pageSize) {
    this.handler   = handler;
    this.capacity  = capacity;
    this.pageSize  = pageSize;
    }


    //private int recordCount;

    public int getRecordCount()
    throws SQLException {
    return handler.getRecordCount();
    }

    public int getPageCount() 
    throws SQLException {
    //Fetch record count
    int recordCount = getRecordCount();

    if (recordCount % pageSize != 0) {
    return (int) (recordCount / pageSize) + 1;
    }
    else {
    return recordCount / pageSize;
    }
    }

    public List getNextPage()
    throws SQLException {
    List retList = new ArrayList();

    // fetch limited number records from currentIndex
    if (currentIndex + pageSize <= endIndex) {
    retList.addAll(records.subList(currentIndex, currentIndex + pageSize));
    }
    // If the cursor is near to endIndex
    else {
    retList.addAll(records.subList(currentIndex, endIndex));
    getBlock(endIndex, capacity);

    }
    // 
    return retList;
    }

    public List getPage(int pageIndex) {
    List retList = new ArrayList();

    return retList;
    }

    public boolean hasNext()
    throws SQLException {

    return currentIndex < getRecordCount();
    }



    private void getBlock(int startIndex, int num) 
    throws SQLException {
    //Fetch records 
    records = handler.getBlock(startIndex, num);
    //Update the startIndex and endIndex
    this.startIndex = startIndex;
    this.endIndex = startIndex + records.size();
    }
    ///////////////////////Test////////////////////

    private static void p(Object obj) {
    System.out.println(obj.toString());
    }

    public static void main(String[] args) {
    PaginationManager manager = new PaginationManager(null);
    }
    }