[Java代码] JDBC分页工具类
2015-11-26 10:27
537 查看
package com.iflytek.page;
/**
* 分页工具类
*
* @author xudongwang 2012-1-19
*
* Email:xdwangiflytek@gmail.com
*/
public class Page {
/**
* 总记录数
*/
private int totalRow;
/**
* 每页记录数
*/
private int pageSize = 10;
/**
* 当前页码
*/
private int currentCount;
/**
* 总页数
*/
private int total;
/**
* 起始记录下标
*/
private int beginIndex;
/**
* 截止记录下标
*/
private int endIndex;
/**
* 构造方法,使用总记录数,当前页码
*
* @param totalRow
* 总记录数
* @param currentCount
* 当前页面,从1开始
*/
public Page(int totalRow, int currentCount) {
this.totalRow = totalRow;
this.currentCount = currentCount;
calculate();
}
/**
* 构造方法 ,利用总记录数,当前页面
*
* @param totalRow
* 总记录数
* @param currentCount
* 当前页面
* @param pageSize
* 默认10条
*/
public Page(int totalRow, int currentCount, int pageSize) {
this.totalRow = totalRow;
this.currentCount = currentCount;
this.pageSize = pageSize;
calculate();
}
private void calculate() {
total = totalRow / pageSize + ((totalRow % pageSize) > 0 ? 1 : 0);
if (currentCount > total) {
currentCount = total;
} else if (currentCount < 1) {
currentCount = 1;
}
beginIndex = (currentCount - 1) * pageSize;
endIndex = beginIndex + pageSize;
if (endIndex > totalRow) {
endIndex = totalRow;
}
}
public int getTotalRow() {
return totalRow;
}
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);
复制代码
数据访问层, StduentDao.java
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;
}
复制代码
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 |
*/
http://www.kmnk01.com/hxpfk/2015/myzj_1125/109.html |
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 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 currentCount
http://www.kmnk01.com/hxpfk/2015/zlff_1125/117.html |
*/
public Page(int totalRow, int currentCount) {
http://www.kmnk01.com/hxpfk/2015/yydt_1125/119.html |
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 |
}
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);
复制代码
数据访问层, StduentDao.java
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;
}
复制代码
相关文章推荐
- Spring MVC 多文件上传大小限制及异常处理
- java多线程之Executors线程池
- android NDK 学习笔记(3)---eclipse 环境自动创建头文件.h ---javah
- 最全 Eclipse 快捷键
- Spring配置事务
- 在struts2中重定向加参数
- java笔记--关于线程同步(5种同步方式)【转】
- java调用外部exe程序并传参
- JAVA的数据存储
- 解决Maven工程中报 Missing artifact jdk.tools:jdk.tools:
- Spring MVC+MyBatis框架使用多个数据源以及之间的切换
- java cookies session 的思考。
- Java算法---发工资
- eclipse报错:发现了以元素 'd:skin' 开头的无效内容。此处不应含有子元素
- Java并发编程:线程池的使用
- Spring REST for DELETE Request Method Not Supoorted
- Java算法---杨辉三角的打印输出
- Spring注解@Component、@Repository、@Service、@Controller区别
- spring源码之—Assert.notNull-断言
- 如何同时激活两个不同版本的MyEclipse 【MyEclipse2013和MyEclipse2014同时激活】