JSP Pagination1

Embed Size (px)

Citation preview

  • 8/6/2019 JSP Pagination1

    1/5

    JSP Pagination

    Fetching thousand and millions of records from database is big time consuming and consumes more almost all CPU power

    with memory of machine.

    If we break thousand of records into small chunks (Pages) with showing 10 or 25 some limited number of records, it will

    load records very faster and smoothly.

    Pagination in JSP achieves by total number of rows and with limit the number of rows in ResultSet. If numbers of records

    are larger than limit specified rows, it will automatically make page index. It is like google pagination or google pagination

    index.

    User can go through next page by page index number and explore limited records in per pages.

    MySql function SQL_CALC_FOUND_ROWS gives us total number of rows in sql query with limit clause, FOUND_ROWS()

    function fetch number count in result.

  • 8/6/2019 JSP Pagination1

    2/5

    PreparedStatement psPagination=null;PreparedStatement psRowCnt=null;

    int iShowRows=5; // Number of records show on per pageint iTotalSearchRecords=10; // Number of pages index shown

    int iTotalRows=nullIntconv(request.getParameter("iTotalRows"));int iTotalPages=nullIntconv(request.getParameter("iTotalPages"));int iPageNo=nullIntconv(request.getParameter("iPageNo"));int cPageNo=nullIntconv(request.getParameter("cPageNo"));

    int iStartResultNo=0;int iEndResultNo=0;

    if(iPageNo==0){

    iPageNo=0;}else{

    iPageNo=Math.abs((iPageNo-1)*iShowRows);}

    String sqlPagination="SELECT SQL_CALC_FOUND_ROWS * FROM tableName limit"+iPageNo+","+iShowRows+"";

    psPagination=conn.prepareStatement(sqlPagination);rsPagination=psPagination.executeQuery();

    //// this will count total number of rowsString sqlRowCnt="SELECT FOUND_ROWS() as cnt";psRowCnt=conn.prepareStatement(sqlRowCnt);rsRowCnt=psRowCnt.executeQuery();

    if(rsRowCnt.next()){

    iTotalRows=rsRowCnt.getInt("cnt");}

    %>Pagination of JSP page

    NameBatchAddress

  • 8/6/2019 JSP Pagination1

    3/5

  • 8/6/2019 JSP Pagination1

    4/5

    for(i=((cPage*iTotalSearchRecords)-(iTotalSearchRecords-

    1));iiTotalSearchRecords && i >> NextRows - Total Result

  • 8/6/2019 JSP Pagination1

    5/5

    e.printStackTrace();}

    %>