贴一个偶写的分页类 - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- 贴一个偶写的分页类 (http://www.webasp.net/article/8/7468.htm) |
| -- 作者:未知 -- 发布日期: 2003-07-26 |
| 抽象类 ========================================================== import java.util.ArrayList; import java.sql.Connection; import java.sql.ResultSet; import com.xxx.util.DBTool; /** * <pre> * 分页类.默认页面大小为20 * 这是一个抽象类。子类需要重构方法selResult() * </pre> */ public abstract class PageList { /** 以下成员变量在子类中可视 */ protected final static int MIN_PAGE_SIZE = 20; //默认页面大小 protected int pageSize; //页面大小 protected int curPage; //当前页页码 protected int prePage; //上一页页码 protected int nxtPage; //下一页页码 protected int totalPage; //总页码数 protected int totalItem; //总条目数 protected String sql; //选择条件 protected ArrayList result; //结果集 protected int from; //开始的游标位置 /** 私有变量 */ private boolean hasFindResult = false; //标志是否已经查找了结果 //保证loadResult只进行一次 /** * 构造器.默认页面大小为20 * @param sql * @param pageNo */ public PageList(String sql, int pageNo) { init(sql, MIN_PAGE_SIZE, pageNo); } /** * 构造器 * @param sql 条件SQL * @param pageSize 页面大小 * @param pageNo 页码 */ public PageList(String sql, int pageSize, int pageNo) { init(sql, pageSize, pageNo); } protected void init(String sql, int pageSize, int pageNo) { this.sql = sql; this.pageSize = (pageSize<=0?MIN_PAGE_SIZE:pageSize); this.curPage = (pageNo<=0?1:pageNo); } /** * 获取页面大小 */ public int getPageSize() { if (!this.hasFindResult) { this.loadResult(); } return this.pageSize; } /** * 获取当前页码 */ public int getCurPage() { if (!this.hasFindResult) { this.loadResult(); } return this.curPage; } /** * 获取前一页的页码 */ public int getPrePage() { if (!this.hasFindResult) { this.loadResult(); } return this.prePage; } /** * 获取后一页的页码 */ public int getNxtPage() { if (!this.hasFindResult) { this.loadResult(); } return this.nxtPage; } /** * 获取页码总数 */ public int getTotalPage() { if (!this.hasFindResult) { this.loadResult(); } return this.totalPage; } /** * 获取总条数 */ public int getTotalItem() { if (!this.hasFindResult) { this.loadResult(); } return this.totalItem; } /** * 判断是否有页面 * @return 如果总页面数为0,那么返回false;否则true */ public boolean hasPages() { if (!this.hasFindResult) { this.loadResult(); } return (this.totalPage != 0); } /** * 获取指定页面的结果集 * * @return 指定页面结果集;如果没有则返回空集 */ public ArrayList getResult() { if (!this.hasFindResult) { this.loadResult(); } return this.result; } /** * 选出结果 */ protected abstract ArrayList selResult(); /** * 计算页码信息并且得到结果集. * 是calculatePageNoInfo和selResult的组合 */ private void loadResult() { //1.计算页码相关信息 this.calculatePageNoInfo(); //2.选出结果 this.result = this.selResult(); if (this.result == null) { this.result = new ArrayList(); } //3. 保证loadResult()只进行一次 hasFindResult = true; } /** * 计算页码相关信息 calculate */ private void calculatePageNoInfo() { //1. 获取总条目数量 this.totalItem = totalItem(); //2. 计算页码信息 //2.1 总页数 if (this.totalItem == 0) { this.totalPage = 0; } else { this.totalPage = (this.totalItem-1) / this.pageSize + 1; } //2.2 当前页,前页,后页 if (this.totalPage == 0) { this.curPage = 0; this.prePage = 0; this.nxtPage = 0; this.from = 0; } else { this.curPage = (this.curPage>this.totalPage?this.totalPage:this.curPage); this.prePage = ((this.curPage-1<1)?1:(this.curPage-1)); this.nxtPage = ((this.curPage==this.totalPage)?this.curPage:(this.curPage+1)); this.from = (this.curPage-1)*this.pageSize; } } /** * 获取总条目数量 */ private int totalItem() { //access db int count = 0; DBTooldb = new DBTool(); ResultSet rs = null; db.connDB(); rs = db.advQuery(this.sql); try { while (rs.next()) { count++; } } catch (Exception e) { } db.closeDB(); return count; } } //~ 使用 =========================================================== 假设我有一个News实体对象,现在要构造一个News相关的分页类 如下: import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import com.xxx.util.DBTool; import com.bokesoft.util.PageList; import com.bokesoft.util.DateUtil; /** * 新闻分页类 */ public class NewsPageList extends PageList { public NewsPageList(String sql, int pageNo) { super(sql, pageNo); } public NewsPageList(String sql, int pageSize, int pageNo) { super(sql, pageSize, pageNo); } protected java.util.ArrayList selResult() { java.util.ArrayList result = new java.util.ArrayList(this.pageSize); if (this.totalItem == 0) { return result; } News news = null; DBTool db = new DBTool(); ResultSet rs = null; db.connDB(); rs = db.advQuery(this.sql); if (rs != null) { try { //一旦有异常被捕捉就结束循环 int count = 1; if (this.curPage > 1) { rs.absolute(this.from); //跳转到开始行 } while (rs.next() && count<=pageSize) { //循环构造news对象,然后加入列表中 news = new News(); news.setId(rs.getString("code")==null?"":rs.getString("code")); ...... result.add(news); count++; } } catch (Exception e) { } finally { try { if (rs != null) rs.close();} catch (SQLException sqle) {} } } db.closeDB(); return result; } } //~ 客户程序的使用(jsp中) ======================================================== <% //获得跳转的页码和显示条目数量以及选择条件 //... //int iPageNo //int iPageSize //String sqlCond //这里是具体使用分页类的部分,非常简单 NewsPageList npl = new NewsPageList(sqlCond, iPageSize, iPageNo); java.util.ArrayList resultList = npl.getResult(); int totalPage = npl.getTotalPage(); News news = null; int size = resultList.size(); for (int i=0; i<size; i++) { news = (News)resultList.get(i); //..... } %> |
| webasp.net |