java中的分页

分页类
import java.util.ArrayList;
import java.util.List;

public class Pager {

// 页面大小
protected int[] pageSizeList = { 10, 25, 50, 100, 200, 300, 500 };

// 一页显示的记录数
protected int pageSize = Constants.pageSize;

// 当前页码
protected int pageNo = Constants.pageNo;

// 记录总数
protected int rowCount = 0;

// 总页数
protected int pageCount = 1;

// 起始行数
protected int startIndex = 1;

// 结束行数
protected int endIndex = 1;

protected int firstPageNo = 1;
protected int prePageNo = 1;
protected int nextPageNo = 1;
protected int lastPageNo = 1;

// 结果集存放List
protected List resultList;

public Pager(int pageSize, int pageNo, int rowCount, List resultList) {
this.pageSize = pageSize;
this.pageNo = pageNo;
this.rowCount = rowCount;
this.resultList = resultList;

if (rowCount % pageSize == 0) {
this.pageCount = rowCount / pageSize;
} else {
this.pageCount = rowCount / pageSize + 1;
}
this.startIndex = pageSize * (pageNo - 1);
this.endIndex = this.startIndex + resultList.size();

this.lastPageNo = this.pageCount;
if (this.pageNo > 1) this.prePageNo = this.pageNo -1;
if (this.pageNo == this.lastPageNo){
this.nextPageNo = this.lastPageNo;
} else {
this.nextPageNo = this.pageNo + 1;
}
}

public Object[] getPageSizeIndexs() {
List result = new ArrayList(pageSizeList.length);
for (int i = 0; i < pageSizeList.length; i++) {
result.add(String.valueOf(pageSizeList[i]));
}
Object[] indexs = (result.toArray());
return indexs;
}

public Object[] getPageNoIndexs() {
List result = new ArrayList(pageCount);
for (int i = 0; i < pageCount; i++) {
result.add(String.valueOf(i + 1));
}
Object[] indexs = (result.toArray());
return indexs;
}}


数据库的查询

String sql ="select * from 表名 where 条 件 order by id " +
"limit " + (pageNo - 1) * pageSize + ", " + pageSize;

你可能感兴趣的:(java,sql)