分页显示批量数据
此處一共涉及兩個文件:
?
???? 2.pases.jsp負責分頁顯示數據庫中表的數據;
Pages顯示分頁相關 1 <%@ page language="java" contentType="text/html; charset=GB2312 2 pageEncoding="GB2312%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=GB2312> 7 <title>pages</title> 8 </head> 9 <body> 10 <% 11 //分頁變量的定義; 12 final int e=3; //每頁顯示的記錄數 13 int totalPages=0; //頁面總數 14 int currentPage=1; //當前page編號 15 int totalCount=0; //database中數據的所用數據 16 int p=0; //當前pase所顯示的第一條記錄的索引 17 18 //讀取當前待顯示的頁面的編號 19 String tempStr=request.getParameter("CurrentPage"); 20 if (tempStr!=null && !tempStr.equals("")) 21 currentPage=Integer.parseInt(tempStr); 22 23 /*分頁預備*/ 24 25 26 //計算總記錄 27 rs=stmt.executeQuery("select count(*) from datatable;"); 28 if (rs.next()) 29 totalCount=rs.getInt(1); 30 31 //計算總的頁數 32 totalPages=((totalCount%e==0)?(totalCount/e):(totalCount/e+1)); 33 if(totalPages==0) totalPages=1; 34 35 //修正當前page的編號,確保: 1<=currentPage<=totalPages; 36 if(currentPage>totalPages) 37 currentPage=totalPages; 38 else if(currentPag<1) 39 currentPage=1; 40 41 //計算當前page所顯示的第一條記錄索引 42 p=(currentPage-1)*e; 43 44 String sql="select XXX,XXX from datatable order by id limit " +p+",+e"; 45 rs=stmt.executeQuery(sql); 46 %> 47 48 <%-- 顯示page標簽-- %> 49 //yema 50 51 <% 52 for(int i=1;i<=totalPages;i++){ 53 if(i==currentPage) 54 %> 55 <%=i%> 56 57 <% }else{ %> 58 <a href="dbaccess.jsp?curentPage=<%=i%>"><%=i %></a> 59 <% } %> 60 <% } %> 61 62 共<%=totalPages%>頁 ,共<%=totalCount%>條記錄 63 64 65 <table border="1" width=400> 66 67 <tr> 68 <td bgcolor=""> </td> 69 <td bgcolor=""> </td> 70 <td bgcolor=""> </td> 71 <td bgcolor=""> </td> 72 </tr> 73 74 <% 75 while(rs.next()){ 76 String XXX = rs.getString(1); 77 String XXX = rs.getString(2); 78 String XXX = rs.getString(3); 79 float XXX = rs.getfloat(4); 80 81 %> 82 <tr> 83 <td><%=XXX %></td> 84 <td><%=XXX %></td> 85 <td><%=XXX %></td> 86 <td><%=XXX %></td> 87 </tr> 88 89 <% 90 } 91 %> 92 </table> 93 </body> 94 </html>?
轉載于:https://www.cnblogs.com/wocn/archive/2013/01/10/pages_current.html
總結
- 上一篇: JAVA的嵌入式脚本开发(中)
- 下一篇: R 读取excel的方法