码迷,mamicode.com
首页 > Web开发 > 详细

JSP的分页技术

时间:2014-11-24 22:15:34      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   color   os   sp   java   for   

在实际应用中,如果从数据库中查询的记录特别的多,甚至超过了显示屏的显示范围,这个时候可将结果进行分页显示。

  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文件,代码如下

  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、bubuko.com,布布扣

2、bubuko.com,布布扣

3、bubuko.com,布布扣


数据库代码为:

    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的分页技术

标签:blog   http   io   ar   color   os   sp   java   for   

原文地址:http://www.cnblogs.com/baolibin528/p/4119675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!