㈠ 怎樣用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;
}
}