通过JSP程序来实现的分页

网络整理 - 07-27

通过jsp(SUN企业级应用的首选)程序获得database里的数据,然后通过程序控制其显示方式,这种方式明显缺点是如果数据量比较大的时候会有明显的性能下降
    现将测试代码奉上,尽管我尽量限制代码的长度

    先说下开发步骤;DB脚本,测试获得的数据是否正确的显示在一个table中,添加那些该显示那些不该显示,添加了首叶上叶下叶尾叶的功能,添加当前位置标签比如4/5,添加直接跳转到某页的功能,处理掉一些bug。。

1。先写数据库端测试脚本:

--删除表
drop table person;

--建立表
create table person (
 id int auto_increment not null Prima(最完善的虚拟主机管理系统)ry key ,
 uid  varchar(32),
 name varchar(32),
 password varchar(20)
);
--插入数据?不支持中文,必须指定表内投影,插入多于30条记录用于测试分页
insert into person(uid,name,password) values('uid1','testname','123456');
insert into person(uid,name,password) values('uid1','testname','123456');
insert into person(uid,name,password) values('uid1','testname','123456');
insert into person(uid,name,password) values('uid1','testname','123456');
insert into person(uid,name,password) values('uid1','testname','123456');
insert into person(uid,name,password) values('uid1','testname','123456');
insert into person(uid,name,password) values('uid1','testname','123456');
insert into person(uid,name,password) values('uid1','testname','123456');
insert into person(uid,name,password) values('uid1','testname','123456');
--事务提交

2。整个程序部分。

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page import="java.sql.*"%>
<html>
<head>
    <title>jsp(SUN企业级应用的首选)通过程序来控制分页</title>
    <script type="text/javascript">
            function openPage(currentpage) {
                document.getElementById("cp").value = currentpage;
                document.getElementById("frm").submit();

            }
            function changePage() {
            document.getElementById("cp").value = document.getElementById("selNumber").value ;
            document.getElementById("frm").submit();
            }
    </script>
</head>
<body>
    
      <%
        int pageSize = 10 ;
        int currentPage = 1 ;

        int allRecorder = 0 ;
        int pageNumber  ;

        try {
            currentPage = Integer.parseInt(request.getParameter("cp"))  ;
        } catch(Exception ex ) {
          
        }
        //关于数据库连接部分的处理根据自己的数据库来指定
        final String DBDriver = "org.gjt.mm.MySQL(和PHP搭配之最佳组合).Driver";
  final String DbUrl = "jdbc:MySQL(和PHP搭配之最佳组合)://localhost/test";
  final String DBUser = "root";
  final String DBPSW = "root";
  Connection conn = null;
  try {
   Class.forName(DBDriver);
   conn = DriverManager.getConnection(DbUrl,DBUser,DBPSW);
            String sql = "select count(id) from person";
            PreparedStatement pst = conn.prepareStatement(sql);
            ResultSet rs = pst.executeQuery();
            if(rs.next()) {
                  allRecorder = rs.getInt(1);
            }
            pageNumber = (allRecorder+pageSize-1)/pageSize ; //得到尾页数
            rs.close();
            pst.close();

            sql = "select id,uid,name,password from person";
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
      %>
      <form action="test.jsp(SUN企业级应用的首选)" method="get" id="frm">
          <input type="button" value="首页"   onclick="openPage(1);" <%=currentPage==1?"disabled":""%> />
          <input type="button" value="上页"   onclick="openPage(<%=currentPage-1%>);" <%=currentPage==1?"disabled":""%>/>
          <input type="button" value="下页"   onclick="openPage(<%=currentPage+1%>);" <%=currentPage==pageNumber?"disabled":""%>/>
          <input type="button" value="尾页"   onclick="openPage(<%=pageNumber%>);" <%=currentPage==pageNumber?"disabled":""%>/>
          <font color="red"><%=currentPage%>/<%=pageNumber%></font>
          <input type="hidden" value="" name="cp">
          <select id="selNumber" onchange="changePage();">
              <%
                  for(int temp=1;temp<=pageNumber;temp++) {
              %>
                        <option value="<%=temp%>" <%=currentPage==temp?"selected":""%> ><%=temp%></option>
              <%         
                  }
              %>
          </select>
      </form>
      <table border=1 width=80%>
  <tr>
   <td>编号</td>
   <td>登陆名称</td>
   <td>姓名</td>
   <td>密码</td>
   <td colspan=2>操作</td>
  </tr>
  <%
        for(int temp = 0;temp<pageSize*(currentPage-1);temp++) {
            rs.next();   //需要空出去部分代码不显示      
        }

        boolean hasData = false;
        for(int temp=0 ;temp<pageSize;temp++) {
            if(rs.next()) {
                hasData = true ;
        %>
                <tr>
                <td><%=rs.getInt(1)%></td>
                <td><%=rs.getString(2)%></td>
                <td><%=rs.getString(3)%></td>
                <td><%=rs.getString(4)%></td>
                <td >修改</td>
                <td >删除</td>
                </tr>
  <%
      }
        }
            if(!hasData) {%>
      <tr><td colspan=6> 数据不存在</td></tr>
  <%
  }
  %>
      </table>
      <%
            rs.close();
         pst.close();
      conn.close();
     }catch(Exception ex) {
    out.print("数据错误");
   }
      %>
</body>
</html>