㈠ 怎样用java实现分页显示
实现原理很简单,就是建立一个Page类,里面放当前访问的页数(这个是从客户浏览器传到后台的数据,所以你的分页需要用它来定位记录的条目)和每一页显示的记录行数。然后通过分页计算就可以得出下列数据。
(假定你的页数从1开始)
1、总页数 = 总记录数/每页大小,如果0!=总记录数%每页大小,那么总页数再+1
2、当前页数(从浏览器传递的参数中获得)
3、表记录的起始位置=(当前页数-1)*每页大小
4、总记录数(select count(*) from [表名] [where [条件]],从数据库中查询得到)
5、每页大小,可以固定,也可以从页面传过来
有了这几个参数之后,就用sql语句查出对应的记录就可以了。
mysql数据库用limit 表记录的起始位置,每页大小 语句添加到你的查询语句最后面
sqlserver数据库用top语句和not in 来做
oracle数据库用rownum来做
再给你一段分页对象代码,你自己先读一下
publicclassPage{
privatelongtotalCount=0;//总记录数
privateintpageNumber=1;//当前页号,默认显示第一页
privateintpageSize=20;//每页大小,默认每页20条
privateinttotalPage=0;//总页数,默认为0
privateintstartRow=0;//起始记录行号,默认为从表头开始
/**
*分页计算方法,由setTotalCount调用
*/
publicvoidpagination(){
//计算总页数
if(this.totalCount%pageSize==0)
this.totalPage=newLong(this.totalCount/pageSize).intValue();
else
this.totalPage=newLong(this.totalCount/pageSize).intValue()+1;
//排除错误页号
if(this.pageNumber<1)
this.pageNumber=1;
if(this.pageNumber>this.totalPage)
this.pageNumber=this.totalPage;
//计算起始行号
this.startRow=(this.pageNumber-1)*this.pageSize;
}
publiclonggetTotalCount(){
returntotalCount;
}
publicvoidsetTotalCount(longtotalCount){
this.totalCount=totalCount;
this.pagination();
}
publicintgetPageNumber(){
returnpageNumber;
}
publicvoidsetPageNumber(intpageNumber){
this.pageNumber=pageNumber;
}
publicintgetPageSize(){
returnpageSize;
}
publicvoidsetPageSize(intpageSize){
this.pageSize=pageSize;
}
publicintgetTotalPage(){
returntotalPage;
}
publicvoidsetTotalPage(inttotalPage){
this.totalPage=totalPage;
}
publicintgetStartRow(){
returnstartRow;
}
publicvoidsetStartRow(intstartRow){
this.startRow=startRow;
}
}