学习了数据库连接池后,我们发现除了把数据显示出来后,如果数据过大,一次性全部显示,页面会显得很臃肿,所以我们引进了分页技术,来实现分页效果。运用c3p0和dbutil的技术能够把数据用sql的limit语句实现分页
首先,核心的是创建一个分页类,PageBean.class
package example.entity; import java.util.List; public class Pagebean { private int firstPage =1; //首页 private int prePage; //上一页 private int nextPage; //下一页 private int totalPage; //末页/总页数 private int curPage; //当前页 private List datas; //需要显示的数据 private int curSize =3; //每页显示的数据 private int totalSize; // 总的记录数 public int getFirstPage() { return firstPage; } public void setFirstPage(int firstPage) { this.firstPage = firstPage; } public int getPrePage() { return this.getCurPage()==this.getFirstPage()?this.getFirstPage():this.getCurPage()-1; } public void setPrePage(int prePage) { this.prePage = prePage; } public int getNextPage() { return this.getCurPage()==this.getTotalPage()?this.getTotalPage():this.getCurPage()+1; } public void setNextPage(int nextPage) { this.nextPage = nextPage; } public int getTotalPage() { return this.getTotalSize()%this.getCurSize()==0?this.getTotalSize()/this.getCurSize():this.getTotalSize()/this.getCurSize()+1; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getCurPage() { return curPage; } public void setCurPage(int curPage) { this.curPage = curPage; } public List getDatas() { return datas; } public void setDatas(List datas) { this.datas = datas; } public int getCurSize() { return curSize; } public void setCurSize(int curSize) { this.curSize = curSize; } public int getTotalSize() { return totalSize; } public void setTotalSize(int totalSize) { this.totalSize = totalSize; } }
然后,定义一个数据访问层dao
package example.dao; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import example.entity.Student; import example.util.c3p0util; public class studentdao {
<span style="white-space:pre"> </span>//查询数据库的总记录数 public int querycount() throws Exception{ QueryRunner qr = new QueryRunner(c3p0util.getData()); Long l = qr.query("select count(*) from student", new ScalarHandler()); return l.intValue(); } //计算每页显示的数据数 public List query(int curpage,int cursize) throws Exception{ QueryRunner qr = new QueryRunner(c3p0util.getData()); return qr.query("select * from student limit ?,?", new BeanListHandler(Student.class), (curpage-1)*cursize,cursize); } }
定义一个Servlet来接受参数
package mjf.page.web; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import mjf.page.dao.StudentDao; import mjf.page.entity.PageBean; public class QueryServlet extends HttpServlet { StudentDao dao = new StudentDao(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PageBean pb = new PageBean(); //计算总记录数 pb.setTotalsize(dao.queryCount()); //计算当前页 String curpage = request.getParameter("curpage"); if(curpage==null || curpage.equals("")){ curpage="1"; } pb.setCurpage(Integer.parseInt(curpage)); //计算每页显示的数据 pb.setDatas(dao.queryfindAll(pb.getCurpage(), pb.getCursize())); request.setAttribute("pb", pb); request.getRequestDispatcher("/index.jsp").forward(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <c:forEach items="${pb.datas }" var="pb"> <table border="1px"> <tr> <th>编号</th> <th>姓名</th> <th>性别</th> <th>部门</th> <th>薪水</th> </tr> <tr> <td>${pb.id }</td> <td>${pb.name }</td> <td>${pb.gender }</td> <td>${pb.dept }</td> <td>${pb.salary }</td> </tr> </table> </c:forEach> <a href="<c:url value='/QueryServlet?curpage=${pb.firstpage }' />" >首页</a> <a href="<c:url value='/QueryServlet?curpage=${pb.prepage }' />" >上一页</a> <a href="<c:url value='/QueryServlet?curpage=${pb.nextpage }' />" >下一页</a> <a href="<c:url value='/QueryServlet?curpage=${pb.lastpage }' />" >尾页</a> <form action="<c:url value='/QueryServlet' />"> 跳转到 <input type="text" name="curpage" size="2" />页 <input value="跳转" type="submit" /> </form> 跳转到 <input type="text" name="pageno" id="pagenoId" size="2" />页 <input value="跳转" type="button" onclick="tiaozhuan()" /> 当前为${pb.curpage }页 /共有${pb.lastpage }页 每页有${pb.cursize }数据 总记录数${pb.totalsize } </body> <script type="text/javascript"> function tiaozhuan(){ var pageid = document.getElementById("pagenoId"); var url = "<c:url value='/QueryServlet?curpage="+pageid+"' />"; window.location.href = url; } </script> </html>
这样的简单小示例能够跟大家分享一下分页的效果,希望能够帮助到大家的理解。实现分页的最主要的就是要定义一个分页类,和实现上一页,下一页,总记录数和每页显示数的实现。