• 假分页与真分页


    一、假分页

    View Code
      1 <%@ page contentType="text/html" pageEncoding="GBK"%>
    2 <%@ page import="java.sql.*"%>
    3 <h1 align="center">雇员信息查询</h1>
    4 <%!
    5 String url="split_page_false.jsp";
    6 %>
    7 <%
    8 int currentPage=1; //显示当前页数
    9 int lineSize=5; //显示每页记录数
    10 int allRecorders=0;//全部的记录数
    11 int pageSize=1; //表示总的页数
    12 int lineData[]={1,3,5,7,9,10};
    13 %>
    14 <%
    15 try
    16 {
    17 currentPage=Integer.parseInt(request.getParameter("cp"));
    18 lineSize =Integer.parseInt(request.getParameter("ls"));
    19 }
    20 catch(Exception ex)
    21 {
    22 ex.printStackTrace();
    23 }
    24 %>
    25 <%
    26 String DBDRIVER="oracle.jdbc.driver.OracleDriver";
    27 String DBURL="jdbc:oracle:thin:@localhost:1521:study";
    28 String DBUSER="scott";
    29 String DBPASSWORD="tiger";
    30 Connection conn=null;
    31 PreparedStatement psmt=null;
    32 ResultSet rst=null;
    33 Class.forName(DBDRIVER);
    34 String sql="select count(empno) from emp";
    35 try
    36 {
    37 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
    38 psmt=conn.prepareStatement(sql);
    39 rst=psmt.executeQuery();
    40 if(rst.next())
    41 {
    42 allRecorders=rst.getInt(1);
    43 }
    44
    45 pageSize=(allRecorders+lineSize-1)/lineSize; //计算总的页数
    46 }
    47 catch(Exception ex)
    48 {
    49 ex.printStackTrace();
    50 }
    51 %>
    52 <script language="javaScript">
    53 function go(num)
    54 {
    55 document.getElementById("cp").value=num;
    56 document.pageForm.submit();
    57 }
    58 </script>
    59 <center>
    60 <form name="pageForm" action="<%=url%>" method="POST">
    61 <input type="button" value="首页" onClick="go(1)" <%=currentPage==1?"disabled":""%>/>
    62 <input type="button" value="上一页" onClick="go(<%=currentPage-1%>)" <%=currentPage==1?"disabled":""%>/>
    63 <input type="button" value="下一页" onClick="go(<%=currentPage+1%>)" <%=currentPage==pageSize?"disabled":""%>/>
    64 <input type="button" value="尾页" onClick="go(<%=pageSize%>)" <%=currentPage==pageSize?"disabled":""%>/>
    65 <input type="hidden" value="1" name="cp"/>
    66
    67 跳转至 第<select name="sect" onChange="go(this.value)">
    68 <%for(int i=1;i<pageSize+1;i++)
    69 {
    70 %>
    71 <option value="<%=i%>" <%=i==currentPage?"selected":""%>><%=i%></option>
    72 <%
    73 }
    74 %>
    75 </select>
    76 每页显示<select name="ls" onChange="go(1)">
    77 <%for(int j=0;j<lineData.length;j++)
    78 {
    79 %>
    80 <option value="<%=lineData[j]%>" <%=lineData[j]==lineSize?"selected":""%>><%=lineData[j]%></option>
    81 <%
    82 }
    83 %>
    84 </select>
    85 条记录
    86
    87 </form>
    88 </center>
    89 <table border="1" width="100%">
    90 <tr>
    91 <th>雇员编号</th>
    92 <th>雇员姓名</th>
    93 <th>雇员岗位</th>
    94 <th>领导编号</th>
    95 <th>雇佣日期</th>
    96 <th>工资</th>
    97 <th>奖金</th>
    98 <th>部门编号</th>
    99 </tr>
    100 <%
    101 String sql2="Select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp";
    102 try
    103 {
    104 psmt=conn.prepareStatement(sql2);
    105 rst=psmt.executeQuery();
    106 for(int i=0;i<(currentPage-1)*lineSize;i++)
    107 {
    108 rst.next();
    109 }
    110 for(int i=0;i<lineSize;i++)
    111 {
    112 if(rst.next())
    113 {
    114 int empno=rst.getInt(1);
    115 String ename=rst.getString(2);
    116 String job=rst.getString(3);
    117 int mgr=rst.getInt(4);
    118 Date hiredate=rst.getDate(5);
    119 float sal=rst.getFloat(6);
    120 float comm=rst.getFloat(7);
    121 int deptno=rst.getInt(8);
    122 %>
    123 <tr>
    124 <td align="center"><%=empno%></td>
    125 <td align="center"><%=ename%></td>
    126 <td align="center"><%=job%></td>
    127 <td align="center"><%=mgr%></td>
    128 <td align="center"><%=hiredate%></td>
    129 <td align="center"><%=sal%></td>
    130 <td align="center"><%=comm%></td>
    131 <td align="center"><%=deptno%></td>
    132 </tr>
    133 <%
    134 }
    135 }
    136 }
    137 catch(Exception ex)
    138 {
    139 ex.printStackTrace();
    140 }
    141 finally
    142 {
    143 try
    144 {
    145 conn.close();
    146 }
    147 catch(Exception ex)
    148 {
    149 ex.printStackTrace();
    150 }
    151 }
    152 %>
    153
    154
    155 </table>
    View Code
      1 <%@ page contentType="text/html" pageEncoding="GBK"%>
    2 <%@ page import="java.sql.*"%>
    3
    4 <%
    5 String url="split_page.jsp";
    6 int currentPage =1; //当前的页数
    7 int lineSize=4; //每页的记录数
    8 int allRecord=0; //总记录数
    9 int pageSize=1; //总页数
    10 int[] lineData={1,3,5,7,9,10};
    11 try
    12 {
    13 currentPage=Integer.parseInt(request.getParameter("cp"));
    14 lineSize=Integer.parseInt(request.getParameter("ls"));
    15 }
    16 catch(Exception ex)
    17 {
    18 ex.printStackTrace();
    19 }
    20 %>
    21 <%
    22 String DBDRIVER="org.gjt.mm.mysql.Driver";
    23 String DBURL="jdbc:mysql://localhost:3306/study_java";
    24 String DBUSER="root";
    25 String DBPASSWORD="mysqladmin";
    26
    27 Connection conn=null;
    28 PreparedStatement psmt=null;
    29 ResultSet rst=null;
    30 Class.forName(DBDRIVER);
    31 try
    32 {
    33 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
    34 String sql1="select count(empno) from emp";
    35 psmt=conn.prepareStatement(sql1);
    36 rst=psmt.executeQuery();
    37 if(rst.next())
    38 {
    39 allRecord=rst.getInt(1);
    40 }
    41 pageSize=(allRecord+lineSize-1)/lineSize;
    42 }
    43 catch(Exception ex)
    44 {
    45 ex.printStackTrace();
    46 }
    47 %>
    48 <script language="javascript">
    49 function go(num)
    50 {
    51 document.getElementById("cp").value=num;
    52 document.pageForm.submit();
    53 }
    54 </script>
    55 <center>
    56 <h1 align="center">雇员信息查询列表</h1>
    57 <form name="pageForm" action="<%=url%>" method="post">
    58 <input type="button" value="首页" onClick="go(1)" <%=currentPage==1?"DISABLED":""%>/>
    59 <input type="button" value="上一页" onClick="go(<%=currentPage-1%>)" <%=currentPage==1?"DISABLED":""%>/>
    60 <input type="button" value="下一页" onClick="go(<%=currentPage+1%>)" <%=currentPage==pageSize?"DISABLED":""%>/>
    61 <input type="button" value="尾页" onClick="go(<%=pageSize%>)" <%=currentPage==pageSize?"DISABLED":""%>/>
    62 跳转至 第<select onChange="go(this.value)">
    63 <%for(int i=1;i<=pageSize;i++)
    64 {
    65 %>
    66 <option value="<%=i%>" <%=currentPage==i?"selected":""%>><%=i%></option>
    67 <%
    68 }
    69 %>
    70 </select>
    71
    72 每页显示
    73 <select name="ls" onChange="go(1)">
    74 <%
    75 for(int i=0;i<lineData.length;i++)
    76 {
    77 %>
    78
    79 <option value="<%=lineData[i]%>" <%=lineData[i]==lineSize?"selected":""%>><%=lineData[i]%></option>
    80 <%
    81 }
    82 %>
    83 </select>
    84 条记录
    85 <input type="hidden" value="1" name="cp" />
    86 </form>
    87 <table border="1" width="100%">
    88 <tr>
    89 <th align="center">雇员编号</th>
    90 <th align="center">雇员姓名</th>
    91 <th align="center">雇员职位</th>
    92 <th align="center">雇佣日期</th>
    93 <th align="center">雇员工资</th>
    94 <th align="center">雇员奖金</th>
    95 </tr>
    96 <%
    97 try
    98 {
    99 String sql2="select empno,ename,job,hiredate,sal,comm from emp";
    100 psmt=conn.prepareStatement(sql2);
    101 rst=psmt.executeQuery();
    102 for(int i=0;i<(currentPage-1)*lineSize;i++)
    103 {
    104 rst.next();//相当于一条记录
    105 }
    106 for(int i=0;i<lineSize;i++)
    107 {
    108 if(rst.next())
    109 {
    110 int empno=rst.getInt(1);
    111 String ename=rst.getString(2);
    112 String job=rst.getString(3);
    113 Date hiredate=rst.getDate(4);
    114 float sal=rst.getFloat(5);
    115 float comm=rst.getFloat(6);
    116 %>
    117 <tr>
    118 <td align="center"><%=empno%></td>
    119 <td align="center"><%=ename%></td>
    120 <td align="center"><%=job%></td>
    121 <td align="center"><%=hiredate%></td>
    122 <td align="center"><%=sal%></td>
    123 <td align="center"><%=comm%></td>
    124 </tr>
    125 <%
    126
    127 }
    128 }
    129 }
    130 catch(Exception ex)
    131 {
    132 ex.printStackTrace();
    133 }
    134 finally
    135 {
    136 try
    137 {
    138 conn.close();
    139 }
    140 catch(Exception ex)
    141 {
    142 ex.printStackTrace();
    143 }
    144 }
    145 %>
    146 </table>
    147 </center>


    二、真分页

    View Code
      1 <%@ page contentType="text/html" pageEncoding="GBK"%>
    2 <%@ page import="java.sql.*"%>
    3
    4 <%
    5 String url="split_page_true.jsp";
    6 int currentPage =1; //当前的页数
    7 int lineSize=3; //每页的记录数
    8 int allRecord=0; //总记录数
    9 int pageSize=1; //总页数
    10 int[] lineData={1,3,5,7,9,10};
    11 try
    12 {
    13 currentPage=Integer.parseInt(request.getParameter("cp"));
    14 lineSize=Integer.parseInt(request.getParameter("ls"));
    15 }
    16 catch(Exception ex)
    17 {
    18 ex.printStackTrace();
    19 }
    20 %>
    21 <%
    22 String DBDRIVER="org.gjt.mm.mysql.Driver";
    23 String DBURL="jdbc:mysql://localhost:3306/study_java";
    24 String DBUSER="root";
    25 String DBPASSWORD="mysqladmin";
    26
    27 Connection conn=null;
    28 PreparedStatement psmt=null;
    29 ResultSet rst=null;
    30 Class.forName(DBDRIVER);
    31 try
    32 {
    33 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
    34 String sql1="select count(empno) from emp";
    35 psmt=conn.prepareStatement(sql1);
    36 rst=psmt.executeQuery();
    37 if(rst.next())
    38 {
    39 allRecord=rst.getInt(1);
    40 }
    41 pageSize=(allRecord+lineSize-1)/lineSize;
    42 }
    43 catch(Exception ex)
    44 {
    45 ex.printStackTrace();
    46 }
    47 %>
    48 <script language="javascript">
    49 function go(num)
    50 {
    51 document.getElementById("cp").value=num;
    52 document.pageForm.submit();
    53 }
    54 </script>
    55 <center>
    56 <h1 align="center">雇员信息查询列表</h1>
    57 <form name="pageForm" action="<%=url%>" method="post">
    58 <input type="button" value="首页" onClick="go(1)" <%=currentPage==1?"DISABLED":""%>/>
    59 <input type="button" value="上一页" onClick="go(<%=currentPage-1%>)" <%=currentPage==1?"DISABLED":""%>/>
    60 <input type="button" value="下一页" onClick="go(<%=currentPage+1%>)" <%=currentPage==pageSize?"DISABLED":""%>/>
    61 <input type="button" value="尾页" onClick="go(<%=pageSize%>)" <%=currentPage==pageSize?"DISABLED":""%>/>
    62 跳转至 第<select onChange="go(this.value)">
    63 <%for(int i=1;i<=pageSize;i++)
    64 {
    65 %>
    66 <option value="<%=i%>" <%=currentPage==i?"selected":""%>><%=i%></option>
    67 <%
    68 }
    69 %>
    70 </select>
    71
    72 每页显示
    73 <select name="ls" onChange="go(1)">
    74 <%
    75 for(int i=0;i<lineData.length;i++)
    76 {
    77 %>
    78
    79 <option value="<%=lineData[i]%>" <%=lineData[i]==lineSize?"selected":""%>><%=lineData[i]%></option>
    80 <%
    81 }
    82 %>
    83 </select>
    84 条记录
    85 <input type="hidden" value="1" name="cp" />
    86 </form>
    87 <table border="1" width="100%">
    88 <tr>
    89 <th align="center">雇员编号</th>
    90 <th align="center">雇员姓名</th>
    91 <th align="center">雇员职位</th>
    92 <th align="center">雇佣日期</th>
    93 <th align="center">雇员工资</th>
    94 <th align="center">雇员奖金</th>
    95 </tr>
    96 <%
    97 try
    98 {
    99 String sql2="select empno,ename,job,hiredate,sal,comm from emp limit "+ (currentPage - 1) * lineSize + "," + lineSize;
    100 psmt=conn.prepareStatement(sql2);
    101 System.out.println(sql2);
    102 rst=psmt.executeQuery();
    103 while(rst.next())
    104 {
    105 int empno=rst.getInt(1);
    106 String ename=rst.getString(2);
    107 String job=rst.getString(3);
    108 Date hiredate=rst.getDate(4);
    109 float sal=rst.getFloat(5);
    110 float comm=rst.getFloat(6);
    111 %>
    112 <tr>
    113 <td align="center"><%=empno%></td>
    114 <td align="center"><%=ename%></td>
    115 <td align="center"><%=job%></td>
    116 <td align="center"><%=hiredate%></td>
    117 <td align="center"><%=sal%></td>
    118 <td align="center"><%=comm%></td>
    119 </tr>
    120 <%
    121
    122
    123 }
    124 }
    125 catch(Exception ex)
    126 {
    127 ex.printStackTrace();
    128 }
    129 finally
    130 {
    131 try
    132 {
    133 conn.close();
    134 }
    135 catch(Exception ex)
    136 {
    137 ex.printStackTrace();
    138 }
    139 }
    140 %>
    141 </table>
    142 </center>
    View Code
      1 <%@ page contentType="text/html" pageEncoding="GBK"%>
    2 <%@ page import="java.sql.*"%>
    3 <%
    4 String url="fuck_page.jsp";
    5 %>
    6 <%
    7 int currentPage =1;
    8 int lineSize=3;
    9 int allRecorders=0;
    10 int pageSize=1;
    11 int[] lineData={1,3,5,7,9,10,12,14};
    12 %>
    13 <%
    14 try
    15 {
    16 currentPage=Integer.parseInt(request.getParameter("cp"));
    17 lineSize=Integer.parseInt(request.getParameter("ls"));
    18 }
    19 catch(Exception ex)
    20 {
    21 }
    22 %>
    23 <%
    24 String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ;
    25 String DBURL = "jdbc:oracle:thin:@localhost:1521:study" ;
    26 String DBUSER = "scott" ;
    27 String DBPASSWORD = "tiger" ;
    28 %>
    29 <%
    30 Connection conn=null;
    31 PreparedStatement psmt=null;
    32 ResultSet rst=null;
    33 %>
    34
    35 <%
    36 Class.forName(DBDRIVER);
    37 conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
    38 String sql="select count(empno) from emp";
    39 psmt=conn.prepareStatement(sql);
    40 try
    41 {
    42 rst=psmt.executeQuery();
    43 if(rst.next())
    44 {
    45 allRecorders=rst.getInt(1);
    46 }
    47 pageSize=(allRecorders+lineSize-1)/lineSize;
    48 }
    49 catch(Exception ex)
    50 {
    51 ex.printStackTrace();
    52 }
    53 System.out.println(currentPage);
    54 %>
    55
    56 <center>
    57 <h1>雇员信息列表</h1>
    58 <script language="javascript">
    59 function go(num)
    60 {
    61 document.getElementById("cp").value=num;
    62 document.pageForm.submit();
    63 }
    64 </script>
    65 <form name="pageForm" action="<%=url%>" method="post">
    66 <input type="button" value="首页" onClick="go(1)" <%=currentPage==1?"disabled":""%>/>
    67 <input type="button" value="上一页" onclick="go(<%=currentPage-1%>)" <%=currentPage==1?"disabled":""%>/>
    68 <input type="button" value="下一页" onclick="go(<%=currentPage+1%>)" <%=currentPage==pageSize?"disabled":""%>/>
    69 <input type="button" value="尾页" onclick="go(<%=pageSize%>)" <%=currentPage==pageSize?"disabled":""%>/>
    70 跳转至<select onChange="go(this.value)">
    71 <%for(int i=1;i<=pageSize;i++)
    72 {
    73 %>
    74 <option value=<%=i%> <%=i==currentPage?"selected":""%>><%=i%></option>
    75 <%
    76 }
    77 %>
    78 </select>
    79
    80 每页显示<select name="ls" onChange="go(1)">
    81 <%for(int i=0;i<lineData.length;i++)
    82 {
    83 %>
    84 <option value=<%=lineData[i]%> <%=lineSize==lineData[i]?"selected":""%>><%=lineData[i]%></option>
    85 <%
    86 }
    87 %>
    88 </select>
    89 条记录
    90 <input type="hidden" value="1" name="cp" />
    91 </form>
    92
    93 </center>
    94
    95 <table border="1" width="100%">
    96 <tr>
    97 <th>雇员编号</th>
    98 <th>雇员姓名</th>
    99 <th>雇员职位</th>
    100 <th>领导编号</th>
    101 <th>雇佣日期</th>
    102 <th>雇员工资</th>
    103 <th>雇员奖金</th>
    104 <th>部门编号</th>
    105 </tr>
    106
    107 <%
    108 try
    109 {
    110 sql="select * from (SELECT empno,ename,job,mgr,hiredate,sal,comm,deptno,ROWNUM rn FROM emp WHERE ROWNUM<=? ORDER BY empno) temp where temp.rn>?";
    111 psmt=conn.prepareStatement(sql);
    112 psmt.setInt(1,currentPage*lineSize);
    113 psmt.setInt(2,(currentPage-1)*lineSize);
    114 rst=psmt.executeQuery();
    115 while(rst.next())
    116 {
    117 int empno=rst.getInt(1);
    118 String ename=rst.getString(2);
    119 String job=rst.getString(3);
    120 int mgr=rst.getInt(4);
    121 Date hiredate=rst.getDate(5);
    122 float sal=rst.getFloat(6);
    123 float comm=rst.getFloat(7);
    124 int deptno=rst.getInt(8);
    125 %>
    126 <tr>
    127 <th><%=empno%></th>
    128 <th><%=ename%></th>
    129 <th><%=job%></th>
    130 <th><%=mgr%></th>
    131 <th><%=hiredate%></th>
    132 <th><%=sal%></th>
    133 <th><%=comm%></th>
    134 <th><%=deptno%></th>
    135 </tr>
    136
    137
    138 <%
    139 }
    140 }
    141 catch(Exception ex)
    142 {
    143 ex.printStackTrace();
    144 }
    145 finally
    146 {
    147 try
    148 {
    149 conn.close();
    150 }
    151 catch(Exception ex)
    152 {
    153 ex.printStackTrace();
    154 }
    155 }
    156 %>
    157 </table>




  • 相关阅读:
    解决execjs 调用js 问题
    处理 get请求套字典问题
    js2py js逆向
    前端页面自适应
    newspaper抓新闻
    easygui
    pycharm 安装插件
    scrapy_代理使用
    SQLAlchemy 介绍,建表,及表的操作 (增 删 改 查)
    数据分析之pandas模块下
  • 原文地址:https://www.cnblogs.com/xiongyu/p/2348582.html
Copyright © 2020-2023  润新知