学习实例.文章管理.分页查询类.Page.java
package business; import java.sql.*; public class Page { DBUtil Conn = new DBUtil(); String TableName; String QueryString; String QueryFields; String OrderString; String CountField; int curPage; int RecordsCount; int TotalPage; int PageSize; public String getCountField() { return CountField; } public void setCountField(String countField) { CountField = countField; } public int getCurPage() { return curPage; } public void setCurPage(int curPage) { this.curPage = curPage; } public String getOrderString() { return OrderString; } public void setOrderString(String orderString) { OrderString = orderString; } public int getPageSize() { return PageSize; } public void setPageSize(int pageSize) { PageSize = pageSize; } public String getQueryFields() { return QueryFields; } public void setQueryFields(String queryFields) { QueryFields = queryFields; } public String getQueryString() { return QueryString; } public void setQueryString(String queryString) { QueryString = queryString; } public int getRecordsCount() { return RecordsCount; } public void setRecordsCount(int recordsCount) { RecordsCount = recordsCount; } public String getTableName() { return TableName; } public void setTableName(String tableName) { TableName = tableName; } public int getTotalPage() { return TotalPage; } public void setTotalPage(int totalPage) { TotalPage = totalPage; } public Page(){ QueryFields = "*"; QueryString = ""; CountField = "ID"; OrderString = " Order BY ID DESC"; curPage = 1; PageSize = 10; } public ResultSet PageSplit() throws Exception{ ResultSet rsA = null; try{ //取得记录数与总页数 String sql = "select Count(" + CountField + ") as iCount From " + TableName + " where 1=1" + QueryString + ""; rsA = Conn.Execute(sql); while(rsA.next()){ RecordsCount = rsA.getInt("iCount"); } rsA.close(); TotalPage = RecordsCount / PageSize; if (RecordsCount % PageSize>0){TotalPage++;} if(curPage<=0){curPage=1;} if(curPage>TotalPage){curPage=TotalPage;} //取得列表 sql="SELECT top " + curPage * PageSize + " " + QueryFields + " FROM " + TableName + " where 1=1" + QueryString + " " + OrderString; rsA = Conn.Execute(sql); int iRow = (curPage - 1) * PageSize + 1; if(iRow==0){iRow=1;} if(iRow>1){ rsA.absolute(iRow - 1); } }catch(Exception e){ System.out.println("PageSplit:" + e.getMessage()); } return rsA; } }