zoukankan      html  css  js  c++  java
  • 分页功能

    一、可滚动结果集

     

    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.为了保存当前页位置,则需要把当前页位置设为全局变量。

     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>  
    示例

    运行结果:

  • 相关阅读:
    面试题58 二叉树的下一个结点
    面试题57 删除链表中重复的结点
    面试题56 链表中环的入口结点
    面试题55 字符流中第一个不重复的字符
    面试题54 表示数值的字符串
    面试题50 树中两个结点的最低公共祖先
    面试题53 正则表达式匹配
    面试题52 构建乘积数组
    面试题51 数组中重复的数字
    Qt链接库出错version Qt_5 not defined
  • 原文地址:https://www.cnblogs.com/yang82/p/7291283.html
Copyright © 2011-2022 走看看