一个通用的分页类~

    现在在学jsp,想到怎么去分页。在网上瞄了些分页的方法,貌似有几种。我刚刚开始想的把所有的记录全部放倒一个list里,最后分页拿出记录。虽然能行,但感觉这样做有点不对劲,数据要是很多的话那要多长的list啊~~还有种想法,每次取记录直接在数据库里去一页数据,但各个数据库又不同,不可能为每个数据库写个吧(学校上的数据库太没水准了,教的sql server2000,top 都没仔细讲~~。就是因为sqlserver2000做课程设计还害我电脑中毒了,怨念···)。想了半天,想不出一个不依赖数据库种类的方法,毕竟对java.sql包里的东西还不熟悉。

    后来上网看到一种通用方法,这种方法是在存有数据的ResultSet中,通过里面的方法模拟分页,每次从中取出当前页的数据。把这种方法封装到一个类中,就可以像操作ResultSet一样了。虽然效率上相对来说不很高,但是考虑到重用性和和效率,这应该是一个比较折中的办法吧。当然了,我觉得具体应用还是根据实际情况再做选取。

    既然是学习,仅仅是理解是不行的,要付诸实现才能变成自己的,自己按照这种方法也试着写了一个这样的类,也许细节上不很严谨,毕竟是属于个没经验的学生···,但是分页功能还是实现了。

    先定义了个接口Pageable

public interface Pageable {

	public boolean gotoPage(int curPage) throws SQLException;

	public boolean beforeFirst() throws SQLException;

	public boolean first() throws SQLException;

	public boolean next() throws SQLException;
	
	public boolean isAfterLast();

	public int getTotalPage();

	public int getTotal();

	public int getCurPage();

	public int getPageNum();

}

 再通过Pagination实现:

import java.sql.ResultSet;
import java.sql.SQLException;

public class Pagination implements Pageable {

	protected ResultSet rs = null;
	
	protected int total;	//总记录数
	protected int pageNum;	//每页显示的页数
	protected int curPage;	//当前第几页
	protected int row;		//当前页第一条记录
	protected int rowFlag;	//当前页偏移量
	protected int totalPage;//总页数
	
	/*
	 * 只使用ResultSet的功能,用此构造方法,再用getResultSet()可获得当前ResultSet
	 */
	public Pagination(ResultSet rs) throws SQLException{
		this.rs = rs;
	}
	/*
	 * 分页用此构造方法
	 */
	public Pagination(ResultSet rs,int pageNum) throws SQLException{
		
		this(rs);
		rs.last();
		this.total = rs.getRow();
		this.pageNum = pageNum;
		
		if(this.pageNum <= 0){ 
			throw new SQLException("每页最大行数无效 :" + this.pageNum);
		}
		
		if(total % pageNum == 0){
			this.totalPage = total / pageNum;
		}else{
			this.totalPage = total / pageNum + 1;
		}
	}
	
	
	/* 
	 * 跳到第curPage页
	 */
	public boolean gotoPage(int curPage) throws SQLException{
		this.curPage = curPage;
		
		if(this.curPage > this.totalPage){
			throw new SQLException("无效页" + this.curPage);
		}
		
		this.row = (curPage - 1) * this.pageNum + 1;
		return rs.absolute(row);
	}
	
	/* 
	 * 将光标移到当前页第一条记录前
	 */
	public boolean beforeFirst() throws SQLException{
		
		this.rowFlag = -1;
		return this.rs.absolute(this.row - 1);
	}
	/* 
	 * 将光标移到当前页第一条记录
	 */
	public boolean first() throws SQLException{
		this.rowFlag = 0;
		return this.rs.absolute(this.row);
	}
	
	/* 
	 * 将光标向后移一位
	 */
	public boolean next() throws SQLException{
		
//		//如果偏移量比每页最大页数多,证明到最后一条记录后,不继续向后next
//		if(this.rowFlag > this.pageNum - 1){
//			return false;
//		} 
//		//当最后一页记录条数少于每页最多页数,计算出最后页最多记录条数,不继续向后next
//		if(total % pageNum != 0 && this.curPage == this.totalPage){
//			if(this.rowFlag > this.total % this.pageNum - 1){
//				return false;
//			}
//		}
		this.rowFlag++;
		return rs.next();
	}
	
	/*
	 * 判断光标是否移到最后条记录后
	 */
	public boolean isAfterLast(){
		//如果偏移量比每页最大页数多,证明到最后一条记录后
		if(this.rowFlag > this.pageNum - 1){
			return true;
		}
		//当最后一页记录条数少于每页最多页数,计算出最后页最多记录条数
		if(total % pageNum != 0 && this.curPage == this.totalPage){
			if(this.rowFlag > this.total % this.pageNum - 1){
				return true;
			}
		}
		return false;
	}
	
	/* 
	 * getter ...
	 */
	public ResultSet getResultSet(){
		return rs;
	}
	public int getTotalPage(){
		return this.totalPage;
	}

	public int getTotal(){
		return this.total;
	}
	
	public int getCurPage(){
		return this.curPage;
	}
	
	public int getPageNum(){
		return this.pageNum;
	}
	
}

 

    用完ResultSet记得close()哦!继续学习中...发到博客频道实干什么的??实验下

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值