JSP實現分頁功能

 分頁須知知識點:

(1)JDBC2.0的可滾動結果集。

(2)HTTP GET請求。


一、可滾動結果集


Connection con  = DriverManager.getConnection();

PreparedStatement stmt = con.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);

ResultSet rs = stmt.executeQuery();

常用方法:

(1)rs.absolute(n);        可以將指針跳到第n行。

(2)rs.relative(n);           可以將指針相對向下或向上n行。

(3)rs.first();

(4)rs.last();

(5)int curRow = rs.getRow();    指針指向的當前行


二、功能實現分解


1.計算結果的個數


rs.last();

int size = rs.getRow();

即可得到結果的個數。


2.得到需要分幾頁


如果一頁能夠放5條記錄,則

int pageCount = (size%5==0)?(size/5):(size/5+1);

即可獲得需要分幾頁。


3.控制一頁中規定顯示記錄個數


如果一頁能顯示5條記錄,可以通過使用count進行計數。

int count = 0;

do{

    if(count>=5) break;

    .....

    count++;

}while(rs.next());

通過break語句,能夠使其顯示到超過規定條目就跳出。


4.如何知道當前是第幾頁


通過HTTP get的特點,在地址欄中標明當前地址,如http://.......?curPage=1    表示現在是第一頁。

String tmp = request.getParameter("curPage");
if(tmp==null){
       tmp="1";
}
curPage = Integer.parseInt(tmp);

可以獲得當前頁。

注意:

rs.absolute(1);表示指向第一條記錄;

不存在rs.absolute(0);

rs.absolute((curPage-1)*PAGESIZE+1);      把結果集指針調整到當前頁應該顯示的記錄的開始.

比如如果一頁顯示5條記錄,當前頁是第二頁,則需要把指針調整到6,當前頁是第三頁,則需要把指針調整爲11.


5.點擊首頁、上一頁、下一頁、尾頁的行爲


<a href="multipage.jsp?curPage=<%curPage+1%>" >下一頁</a>

<a href="multipage.jsp?curPage=<%curPage-1%>" >上一頁</a>

<a href="multipage.jsp?curPage=<%pageCount%>" >尾頁</a>

<a href="multipage.jsp?curPage=1" >首頁</a>


6.爲了保存當前頁位置,則需要把當前頁位置設爲全局變量。


綜合代碼:
[html] view plaincopy
  1. <%@ page contentType="text/html" pageEncoding="GB2312" language="java"%>  
  2. <%@ page import="java.sql.*"%>  
  3. <html>  
  4.     <head>  
  5.         <title>hello</title>  
  6.     </head>  
  7.     <body>  
  8.     <table border="1" spacing="2">  
  9. <%!  
  10.     public static final String DRIVER = "com.mysql.jdbc.Driver";  
  11.     public static final String USER = "root";  
  12.     public static final String PASS = "12345";  
  13.     public static final String URL = "jdbc:mysql://localhost:3306/MLDN";  
  14.     public static final int PAGESIZE = 5;  
  15.     int pageCount;  
  16.     int curPage = 1;  
  17. %>  
  18. <%  
  19.     //一頁放5個  
  20.     String user = null;  
  21.     String pass = null;  
  22.     try{  
  23.         Class.forName(DRIVER);  
  24.         Connection con = DriverManager.getConnection(URL,USER,PASS);  
  25.         String sql = "SELECT empno,ename,job,hiredate,sal,comm FROM emp";  
  26.         PreparedStatement stat = con.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);  
  27.         ResultSet rs = stat.executeQuery();  
  28.         rs.last();  
  29.         int size = rs.getRow();  
  30.         pageCount = (size%PAGESIZE==0)?(size/PAGESIZE):(size/PAGESIZE+1);  
  31.         String tmp = request.getParameter("curPage");  
  32.         if(tmp==null){  
  33.             tmp="1";  
  34.         }  
  35.         curPage = Integer.parseInt(tmp);  
  36.         if(curPage>=pageCount) curPage = pageCount;  
  37.         boolean flag = rs.absolute((curPage-1)*PAGESIZE+1);  
  38.         out.println(curPage);  
  39.         int count = 0;  
  40.           
  41.         do{  
  42.             if(count>=PAGESIZE)break;  
  43.             int empno = rs.getInt(1);  
  44.             String ename = rs.getString(2);  
  45.             String job = rs.getString(3);  
  46.             Date hiredate = rs.getDate(4);  
  47.             float sal = rs.getFloat(5);  
  48.             int comm = rs.getInt(6);  
  49.             count++;  
  50.             %>  
  51.         <tr>  
  52.             <td><%=empno%></td>  
  53.             <td><%=ename%></td>  
  54.             <td><%=job%></td>  
  55.             <td><%=hiredate%></td>  
  56.             <td><%=sal%></td>  
  57.             <td><%=comm%></td>  
  58.         </tr>  
  59.             <%  
  60.         }while(rs.next());  
  61.         con.close();  
  62.     }  
  63.     catch(Exception e){  
  64.           
  65.     }  
  66. %>  
  67. </table>  
  68. <a href = "multipage.jsp?curPage=1" >首頁</a>  
  69. <a href = "multipage.jsp?curPage=<%=curPage-1%>" >上一頁</a>  
  70. <a href = "multipage.jsp?curPage=<%=curPage+1%>" >下一頁</a>  
  71. <a href = "multipage.jsp?curPage=<%=pageCount%>" >尾頁</a>  
  72. <%=curPage%>頁/共<%=pageCount%>頁  
  73.   
  74. </body>  
  75. </html>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章