zoukankan      html  css  js  c++  java
  • JSP---分页

    1、建立java类

    public class StudentManage {
     int pageSize = 3;//每页记录数

    //获取记录总数
    private int getTotalNum(String strWhere) throws SQLException {
    DbAccess dbAccess = new DbAccess();
    int total = 0;
    String sql = "select count(*) from student where 1=1 and " + strWhere;
    dbAccess.excuteQuery(sql);
    ResultSet rs = dbAccess.getRs();
    if (rs.next()) {
     total = rs.getInt(1);
    }
    dbAccess.close();
    return total;
    }

    //获取总页数(即最大的页号)
    public int getMaxPageNo(String strWhere) throws SQLException {
    int maxPageNo;
    if(getTotalNum(strWhere)%pageSize==0)
     maxPageNo=getTotalNum(strWhere)/pageSize;
    else {
     maxPageNo=getTotalNum(strWhere)/pageSize+1;
    }
    return maxPageNo;
    }

    // 获取指定页的记录
    public List<Student> StudList(String strWhere, int page)
    throws SQLException {
    DbAccess dbAccess = new DbAccess();
    List<Student> list = new ArrayList<Student>();
    int total=getTotalNum(strWhere);
    String sql = "select * from student where 1=1 and " + strWhere;
    dbAccess.excuteQuery(sql);
    ResultSet rs = dbAccess.getRs();
    int t = (page - 1) * pageSize + 1;//当前页的首记录号
    if (t <= total) {
     rs.absolute(t);//需要修改DAL层 st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
     do {
      Student student = new Student();
      student.setStud_id(rs.getString("stud_id"));
      student.setStud_name(rs.getString("stud_name"));
      student.setStud_sex(rs.getString("stud_sex"));
      student.setStud_class(rs.getString("stud_class"));
      student.setStud_fav(rs.getString("stud_fav"));
      list.add(student);
      } while (rs.next() && list.size() < pageSize);
     rs.close();
     }
     dbAccess.close();
     return list;
    }

    2、jsp页面调用

    <%
    String curPage = request.getParameter("curPage");
    if (curPage == null || curPage == "") {
     curPage = "1";
    }
    int cp = Integer.parseInt(curPage);
    StudentManage studentManage = new StudentManage();
    int lastPage = studentManage.getMaxPageNo("1=1");
    List<Student> list = studentManage.StudList("1=1", cp);
    %>

    当前第<%=cp%>/<%=lastPage%>页

    <%
    if (cp > 1) {
    %>
    <a href="?curPage=1">第一页</a>&nbsp;
    <a href="?curPage=<%=cp - 1%>">上一页</a>
    <%
    }
    %>
    <%
    if (cp < lastPage) {
    %>
    <a href="?curPage=<%=cp + 1%>">下一页</a>&nbsp;
    <a href="?curPage=<%=lastPage%>">最后一页</a>
    <%
    }
    %>

  • 相关阅读:
    【转】JAVA内存泄露分析和解决方案及WINDOWS自带查看工具
    windows 下面获得pid
    几个工具的收藏
    转-springboot2新版升级springcloud微服务实战Consul+sleuth+zipkin+Feign/Ribbon+Config+Zuul+Hystrix+Turbine
    【转】windows+gitlab配置ssh key
    telnet端口不通
    layui后台框架的搭建
    用ffmpeg+nginx+海康威视网络摄像头rtsp在手机端和电脑端实现直播
    从零开始搭建基于CEFGlue的CB/S的winform项目
    WinForm使用CefSharp内嵌chrome浏览器
  • 原文地址:https://www.cnblogs.com/beast-king/p/3851346.html
Copyright © 2011-2022 走看看