- package com.iflytek.page;
-
http://www.kmnk01.com/hxpfk/2015/myzj_1125/107.html - /**
- * 分页工具类
- *
- * @author xudongwang 2012-1-19
- *
http://www.kmnk01.com/hxpfk/2015/myzj_1125/108.html - * Email:xdwangiflytek@gmail.com
- */
http://www.kmnk01.com/hxpfk/2015/myzj_1125/109.html - public class Page {
-
http://www.kmnk01.com/hxpfk/2015/myzj_1125/110.html - /**
- * 总记录数
- */
- private int totalRow;
http://www.kmnk01.com/hxpfk/2015/myzj_1125/111.html - /**
- * 每页记录数
- */
http://www.kmnk01.com/hxpfk/2015/zlff_1125/112.html - private int pageSize = 10;
- /**
- * 当前页码
- */
- private int currentCount;
http://www.kmnk01.com/hxpfk/2015/zlff_1125/113.html - /**
- * 总页数
- */
- private int total;
-
http://www.kmnk01.com/hxpfk/2015/zlff_1125/114.html - /**
- * 起始记录下标
- */
- private int beginIndex;
-
http://www.kmnk01.com/hxpfk/2015/zlff_1125/115.html - /**
- * 截止记录下标
- */
- private int endIndex;
- /**
http://www.kmnk01.com/hxpfk/2015/zlff_1125/115.html - * 构造方法,使用总记录数,当前页码
- *
http://www.kmnk01.com/hxpfk/2015/zlff_1125/117.html - * @param totalRow
- * 总记录数
- * @param currentCount
http://www.kmnk01.com/hxpfk/2015/zlff_1125/117.html - * 当前页面,从1开始
- */
- public Page(int totalRow, int currentCount) {
http://www.kmnk01.com/hxpfk/2015/yydt_1125/119.html - this.totalRow = totalRow;
- this.currentCount = currentCount;
- calculate();
http://www.kmnk01.com/hxpfk/2015/yydt_1125/120.html - }
- /**
http://www.kmnk01.com/hxpfk/2015/yydt_1125/121.html - * 构造方法 ,利用总记录数,当前页面
- *
- * @param totalRow
http://www.kmnk01.com/hxpfk/2015/mtbd_1125/122.html - * 总记录数
- * @param currentCount
http://www.kmnk01.com/hxpfk/2015/mtbd_1125/123.html - * 当前页面
- * @param pageSize
- * 默认10条
- */
- public Page(int totalRow, int currentCount, int pageSize) {
- this.totalRow = totalRow;
- this.currentCount = currentCount;
- this.pageSize = pageSize;
http://www.kmnk01.com/hxpfk/2015/xjsb_1125/124.html - calculate();
- }
- private void calculate() {
- total = totalRow / pageSize + ((totalRow % pageSize) > 0 ? 1 : 0);
- if (currentCount > total) {
- currentCount = total;
- } else if (currentCount < 1) {
- currentCount = 1;
http://www.kmnk01.com/hxpfk/2015/xjsb_1125/125.html - }
- beginIndex = (currentCount - 1) * pageSize;
- endIndex = beginIndex + pageSize;
- if (endIndex > totalRow) {
- endIndex = totalRow;
- }
- }
- public int getTotalRow() {
- return totalRow;
http://www.kmnk01.com/hxpfk/2015/xjsb_1125/126.html - }
- public int getPageSize() {
- return pageSize;
- }
- public int getCurrentCount() {
- return currentCount;
- }
- public int getTotal() {
- return total;
- }
- public int getBeginIndex() {
- return beginIndex;
- }
- public int getEndIndex() {
- return endIndex;
- }
- }
继续
在后台获取前台传进来的页码 //从页面取得页码
- int currentPage = 1;
- try {
- currentPage = Integer.parseInt(request.getParameter("currentPage"));
- } catch (Exception ex) {}
- //取得总记录数,创建Page对象
- int totalRow = studentDao.getAllStudents();//通过select count 取得总记录数
- Page page = new Page(totalRow, currentPage);
- studentDao.list(page);
- public List<Stduent> getStudentsByPage(Page page) {
- List<Stduent> students = new ArrayList<Stduent>();
- try {
- String sql = "SELECT id,name,email FROM tbl_stduent";
- Connection conn = null;
- try {
- conn = DbUtil.getConnection();
- Statement statement = conn.createStatement();
- statement.setMaxRows(page.getEndIndex());//关键代码,设置最大记录数为当前页记录的截止下标
- ResultSet resultSet = statement.executeQuery(sql);
- if (page.getBeginIndex() > 0) {
- resultSet.absolute(page.getBeginIndex());//关键代码,直接移动游标为当前页起始记录处
- }
- while (resultSet.next()) {
- Stduent student = new Student();
- ……
- students.add(student);
- }
- resultSet.close();
- statement.close();
- } finally {
- if (conn != null) {
- conn.close();
- }
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return students;
- }