分页查询
page 类代码分页查询代码
1 package com.pb.pager; 2 3 public class Pager { 4 /** 5 * 总页数 6 */ 7 private int totalcount = 0; 8 /** 9 * 页面大小,即每页显示记录数 10 */ 11 private int pageSize = 0; 12 /** 13 * 记录总数 14 */ 15 private int recordCount = 0; 16 17 /** 18 * 当前页号 19 */ 20 private int currPageNo=0; 21 22 public int getCurrPageNo() { 23 if(totalcount==0) 24 return 0; 25 return currPageNo; 26 } 27 28 public void setCurrPageNo(int currPageNo) { 29 if(currPageNo>0) 30 this.currPageNo = currPageNo; 31 } 32 33 public int getTotalcount() { 34 return totalcount; 35 } 36 37 public void setTotalcount(int totalcount) { 38 this.totalcount = totalcount; 39 } 40 41 public int getPageSize() { 42 return pageSize; 43 } 44 45 public void setPageSize(int pageSize) { 46 if (pageSize > 0) 47 this.pageSize = pageSize; 48 } 49 50 public int getRecordCount() { 51 return recordCount; 52 } 53 54 public void setRecordCount(int recordCount) { 55 if (recordCount > 0) 56 this.recordCount = recordCount; 57 } 58 59 // 设置总页数 60 private void setTotalpagecount() { 61 if (this.recordCount % this.pageSize == 0) { 62 this.recordCount = this.recordCount / this.pageSize; 63 } else if (this.recordCount % this.pageSize > 0) { 64 this.recordCount = this.recordCount / this.pageSize + 1; 65 }else{ 66 this.recordCount=0; 67 } 68 } 69 /** 70 * 开始记录数 71 * @return 72 */ 73 public int getStartRow(){ 74 return (currPageNo-1)*pageSize+1; 75 76 } 77 /** 78 * 结束记录数 79 * @return 80 */ 81 public int getEndRow(){ 82 return currPageNo*pageSize; 83 84 } 85 }
分页查询的步骤
(1).确定每页显示的数据数量
(2).计算显示数据的总数量
(3).计算显示的页数=总数量/每页显示的数据数量(+1)
(4)。编写分页查询SQl语句
(5).实现分页查询
使用CallbleStatement执行存储过程
1 public int getTotalcountProc() { 2 int totalCount=0; 3 CallableStatement proc=null; 4 String sql="{call PRO_GETTOTALCOUNT(?)}"; 5 getConnection(); 6 try { 7 proc=con.prepareCall(sql); 8 proc.registerOutParameter(1, Types.INTEGER); 9 proc.execute(); 10 totalCount=proc.getInt(1); 11 } catch (SQLException e) { 12 // TODO Auto-generated catch block 13 e.printStackTrace(); 14 } 15 16 // TODO Auto-generated method stub 17 return totalCount; 18 }
分页显示关键点
确定当前页
设置首页,上一页,下一页,末页的页码
首页和末页的控制
1 <script type="text/javascript"> 2 function page_nav(frm,num){ 3 frm.pageIndex.value=num; 4 frm.submit(); 5 } 6 function jump_to(frm,pegeno){ 7 var regexp=/^\d+$/; 8 if(!regexp.test(pegeno)){ 9 alert("请输入正确数字!"); 10 return false; 11 }else{ 12 page_nav(frm,pegeno); 13 } 14 15 } 16 </script> 17 <% 18 //获取当前页码 19 String currentPage=request.getParameter("pageIndex"); 20 if(currentPage==null) 21 currentPage="1"; 22 int pageIndex=Integer.parseInt(currentPage); 23 //获取新闻记录总数量 24 int total=newsService.getTotalcount(); 25 //每页显示记录数 26 int pagesize=2; 27 /*获取总页数*/ 28 Pager pag=new Pager(); 29 pag.setCurrPageNo(pageIndex); 30 pag.setPageSize(pagesize); 31 pag.setRecordCount(total); 32 int totalpage=pag.getTotalPagecount(); 33 //控制首页和末页 34 if(pageIndex<1){ 35 pageIndex=1; 36 }else if(pageIndex>totalpage){ 37 pageIndex=totalpage; 38 } 39 40 List<News> newsList=newsService.getPageNewsList(pageIndex,pagesize); 41 %> 42 <li>共<%=total%>条记录 <%=currentPage%>/<%=totalpage %>页</li> 43 <% 44 if(pageIndex>1){ 45 %> 46 <a href="javascript:page_nav(document.forms[0],1)">首页</a> 47 <a href="javascript:page_nav(document.forms[0],<%=pageIndex-1%>)">上一页</a> 48 <% 49 }if(pageIndex<totalpage){ 50 %> 51 <a href="javascript:page_nav(document.forms[0],<%=pageIndex+1%>)">下一页</a> 52 <a href="javascript:page_nav(document.forms[0],<%=totalpage %>) ">最后一页</a> 53 <% 54 } 55 %>
表单隐藏域传递代码
<script type="text/javascript"> function page_nav(frm,num){ frm.pageIndex.value=num; frm.submit(); }//go跳转函数 function jump_to(frm,pegeno){ var regexp=/^\d+$/; if(!regexp.test(pegeno)){ alert("请输入正确数字!"); return false; }else{ page_nav(frm,pegeno); } } </script> <form name ="searchForm" id="searchForm" action="/JDBC1/jsp/admin/newsDetailList.jsp" method="post"> <input type="hidden" name="pageIndex" value="1"/> </form> <% if(pageIndex>1){ %> <a href="javascript:page_nav(document.forms[0],1)">首页</a> <a href="javascript:page_nav(document.forms[0],<%=pageIndex-1%>)">上一页</a> <% }if(pageIndex<totalpage){ %> <a href="javascript:page_nav(document.forms[0],<%=pageIndex+1%>)">下一页</a> <a href="javascript:page_nav(document.forms[0],<%=totalpage %>) ">最后一页</a> <% } %>
<span class="page-go-form"><label>跳转至</label>
<input type="text" name="inputPage" id="inputPage" class="page-key" />页
<button type="button" class="page-btn" onClick='jump_to(document.forms[0],document.getElementById("inputPage").value)'>GO</button>
</span>
注意当页面404错误时检查路径是否正确;