<%@page import="com.sun.rowset.CachedRowSetImpl"%>
<%@page import="javax.sql.rowset.CachedRowSet"%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%
//变量声明

    CachedRowSet rt = new CachedRowSetImpl();
    int intPageSize = 2; //一页显示的记录数
    int intRowCount = 0; //记录总数
    int intPageCount = 0; //总页数
    int intPage = 1; //待显示页码
    java.lang.String strOP;
    //取得待显示页码
    strOP = request.getParameter("op");
    if (strOP == null) {//表明在QueryString中没有page这一个参数，此时显示第一页数据
        intPage = 1;
        session.setAttribute("currentpage", intPage);
        ///////
        rt.setUsername("root");
        rt.setPassword("mysql123");
        rt.setUrl("jdbc:mysql://localhost:3306/sample");
        rt.setCommand("SELECT customerid,phone FROM customer");
        rt.setPageSize(intPageSize);
        rt.execute();
        //获取记录总数
        intRowCount = rt.size();
        //记算总页数
        intPageCount = (intRowCount + intPageSize - 1) / intPageSize;

        /////////////
        session.setAttribute("data", rt);
        session.setAttribute("pagecount", intPageCount);

    } else {//将字符串转换成整型
        intPageCount = ((Integer) (session.getAttribute("pagecount"))).intValue();
        rt = (CachedRowSet) session.getAttribute("data");
        intPage = ((Integer) (session.getAttribute("currentpage"))).intValue();
        if (strOP.equals("pre")) {
            intPage = intPage - 1;
            session.setAttribute("currentpage", intPage);
            rt.previousPage();
        } else {
            intPage = intPage + 1;
            session.setAttribute("currentpage", intPage);
            rt.nextPage();
        }
    }
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    </head>
    <body topmargin="0" leftmargin="0" >
        <table width="100%"><tr><td >
                    页次：<%=intPage%>/<%=intPageCount%>页  <%=intPageSize%>条/页  
                </td></tr></table>
        <table width="100%">
            <tr>
                <td width＝50%>ID</td>
                <td width＝50%>电话</td>
            </tr>
            <%
                while (rt.next()) {
                    String id = rt.getString(1);
                    String phone = rt.getString(2);
            %>
            <trwidth>
                <td width＝50%><%=id%></td>
                <td width＝50%><%=phone%></td>
                <tr>
                    <%
                        }
                    %>
                    </table>
                    <%if (intPage > 1) {
                    %>
                <a href="page2.jsp?op=pre">上一页</a>
                <%}%>
                <%if (intPage < intPageCount) {%>
                <a href="page2.jsp?op=last">下一页</a>
                <%}%>
                </body>
                </html>