JSP的分頁技術

來源:互聯網
上載者:User

標籤:blog   http   io   ar   color   os   sp   java   for   

在實際應用中,如果從資料庫中查詢的記錄特別的多,甚至超過了顯示屏的顯示範圍,這個時候可將結果進行分頁顯示。

[java] view plaincopy
  1. 假設總記錄數為intRowCount,每頁顯示的數量為inPageSize,總頁數為intPageCount,那麼總頁數的計算公式如下:  
  2.   
  3. 如果:(intRowCount%intPageSize)>0,則intPageCount=intRowCount/intPageSize+1  
  4.   
  5. 如果:(intRowCount%intPageSize)=0,則intPageCount=intRowCount/intPageSize  
  6.   
  7. 翻頁後顯示intPage頁的內容,將記錄指標移動到(intPage-1)*intPageSize+1  


建立pageBreak.jsp檔案,代碼如下

[java] view plaincopy
  1. <%@ page import="java.sql.*" %>  
  2. <%@ page contentType="text/html" pageEncoding="UTF-8" %>  
  3. <html>  
  4.     <head>  
  5.         <meta http-equiv="Content-Type" content="text/html;charest=UTF-8">  
  6.         <title>JSP分頁瀏覽</title>  
  7.     </head>  
  8.     <body>  
  9.         <div align="center">  
  10.         分頁顯示記錄內容  
  11.         <hr>  
  12.         <table border="1" bgcolor="cccfff" align="center">  
  13.             <tr>  
  14.                 <th>學號</th>  
  15.                 <th>班級</th>  
  16.                 <th>姓名</th>  
  17.                 <th>性別</th>  
  18.                 <th>年齡</th>  
  19.             </tr>  
  20.             <%  
  21.                 String DBDRIVER="org.gjt.mm.mysql.Driver";                //我用的mysql資料庫  
  22.                 String DBURL="jdbc:mysql://localhost:3306/example";  
  23.                 String DBUSER="root";  
  24.                 String DBPASS="mysqladmin";  
  25.                 String sql="select * from paging";  
  26.                   
  27.                 int intPageSize;  //一頁顯示的記錄數  
  28.                 int intRowCount;  //記錄總數  
  29.                 int intPageCount; //總頁數  
  30.                 int intPage;    //待顯示頁碼  
  31.                 int i;  
  32.                 String strPage;  
  33.                 intPageSize=5;   //設定一頁顯示的記錄數  
  34.                 //取得待顯示頁碼  
  35.                 strPage=request.getParameter("page");  
  36.                 if(strPage==null){     //沒有page這個屬性  
  37.                     intPage=1;  
  38.                 }else{  
  39.                     intPage=java.lang.Integer.parseInt(strPage);  
  40.                     if(intPage<1){  
  41.                         intPage=1;  
  42.                     }  
  43.                 }  
  44.                   
  45.                   
  46.                 Connection conn=null;  
  47.                 Statement stmt=null;  
  48.                 ResultSet rs=null;  
  49.                 Class.forName(DBDRIVER);  
  50.                 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);  
  51.                 stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);  
  52.                 rs=stmt.executeQuery(sql);  
  53.                 rs.last();     //游標指向查詢結果集的最後一條記錄  
  54.                 intRowCount=rs.getRow();                //擷取記錄總數  
  55.                 intPageCount=(intRowCount+intPageSize-1)/intPageSize;               //計算總頁數  
  56.                 if(intPage>intPageCount){      //調整待顯示的頁碼  
  57.                     intPage=intPageCount;  
  58.                 }  
  59.                 if(intPageCount>0){  
  60.                     rs.absolute((intPage-1)*intPageSize+1);    //將記錄指標定位在待顯示頁的第一天記錄上  
  61.                 }  
  62.                 //顯示資料  
  63.                 i=0;  
  64.                 while(i<intPageSize&&!rs.isAfterLast()){  
  65.             %>  
  66.                 <tr>  
  67.                     <td><%=rs.getString("stuNumber") %></td>        //從資料庫讀取記錄  
  68.                     <td><%=rs.getString("stuBanji") %></td>  
  69.                     <td><%=rs.getString("stuName") %></td>  
  70.                     <td><%=rs.getString("stuSex") %></td>  
  71.                     <td><%=rs.getString("stuAge") %></td>  
  72.                 </tr>  
  73.             <%  
  74.                 rs.next();  
  75.                 i++;  
  76.                 }  
  77.              %>  
  78.          </table>  
  79.          <hr>  
  80.          <div align="center">  
  81.             第<%=intPage %>頁 共<%=intPageCount %>頁  
  82.                 <%   
  83.                     if(intPage>1){  
  84.                 %>  
  85.                 <a href="pageBreak.jsp?page=<%=intPage-1%>">上一頁</a>  
  86.               <%  
  87.                 }       //下一頁  
  88.                 if(intPage<intPageCount){  
  89.               %>  
  90.               <a href="pageBreak.jsp?page=<%=intPage+1%>">下一頁</a>  
  91.              <%  
  92.                 }  
  93.                 rs.close();  
  94.                 stmt.close();  
  95.                 conn.close();  
  96.               %>  
  97.          </div>  
  98.         </div>  
  99.     </body>  
  100. </html>  


顯示結果如下:

1、

2、

3、


資料庫代碼為:

[java] view plaincopy
    1. create database example;  
    2.   
    3. create table paging(  
    4.     stuNumber  varchar(15),  
    5.     stuBanji  varchar(10),  
    6.     stuName  varchar(50),  
    7.     stuSex  varchar(4),  
    8.     stuAge  int  
    9. );  
    10.   
    11. INSERT INTO paging(stuNumber,stuBanji,stuName,stuSex,stuAge) VALUES (‘2012040101207‘,‘24010105‘,‘鮑禮彬‘,‘男‘,23); 

JSP的分頁技術

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.