1. Package Pagebean
Import java.util.list;/** * Pagination of JavaBean * @author Administrator */public class Pagebean<t> {//current page private in T Pagecode; Total pages//private int totalpage; Total record number private int totalcount; Number of record bars per page private int pageSize; Data displayed per page private list<t> beanlist; public int Getpagecode () {return pagecode; } public void Setpagecode (int pagecode) {this.pagecode = Pagecode; }/** * Call Gettotalpage () to get to the total number of pages * JavaBean Property rules: Totalpage is JavaBean is attribute ${pagebean.totalpage} * @return * /public int gettotalpage () {//calculate int totalpage = totalcount/pagesize; Description divisible if (totalcount% pageSize = = 0) {return totalpage; }else{return totalpage + 1; }}/*public void Settotalpage (int totalpage) {this.totalpage = Totalpage; }*/public int Gettotalcount () {return totalcount; } public void Settotalcount (int totalcount) { This.totalcount = TotalCount; } public int getpagesize () {return pageSize; } public void SetPageSize (int pageSize) {this.pagesize = pageSize; } public list<t> Getbeanlist () {return beanlist; } public void Setbeanlist (list<t> beanlist) {this.beanlist = beanlist; }}
2.Servlet
/** * 获取当前页 *如果用户没有传,默认是第一页,如果传了,就是几 * @param request * @return */ public int getPageCode(HttpServletRequest request){ String pc = request.getParameter("pc"); // 判断 if(pc == null || pc.trim().isEmpty()){ return 1; } return Integer.parseInt(pc); }
/** * 分页查询 */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 处理当前页 * 处理每页显示的记录条数 */ // 当前页 int pageCode = getPageCode(request); // 处理每页显示的记录条数 int pageSize = 4; try { // 调用业务层,分页查询 PageBean<Product> page = new ProductService().findByPage(pageCode,pageSize); // 存入 request.setAttribute("page", page); // 转发 request.getRequestDispatcher("/jsp/pages.jsp").forward(request, response); } catch (SQLException e) { e.printStackTrace(); } }
3.service
/** * 分页查询 * @param pageCode * @param pageSize * @return * @throws SQLException */ public PageBean<Product> findByPage(int pageCode, int pageSize) throws SQLException { return new ProductDao().findByPage(pageCode,pageSize); }
4.dao
/** * 分页查询 * * @param pageCode * @param pageSize * @return * @throws SQLException */ public PageBean<Product> findByPage(int pageCode, int pageSize) throws SQLException { PageBean<Product> page = new PageBean<>(); // 属性是空的 page.setPageCode(pageCode); page.setPageSize(pageSize); QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource()); // ScalarHandler 处理聚合函数 long count = (long) qr.query("select count(*) from product", new ScalarHandler()); // 设置总记录条数 page.setTotalCount((int) count); // limit a,b a = (当前页-1) * b List<Product> beanList = qr.query("select * from product limit ?,?", new BeanListHandler<Product>(Product.class), (pageCode - 1) * pageSize, pageSize); // 每页显示的数据 page.setBeanList(beanList); return page; }
5.utils
public class Jdbcutils {///member variable, created C3P0 connection pool (connection pool already exists ... private static final Combopooleddatasource DATASOURCE = new Combopooleddatasource (); /** * Returns the C3P0 connection pool * @return */public static DataSource Getdatasource () {return DataSource; /** * Gets the connection, returns the connection * @return */public static Connection getconnection () {Connection conn = null; try {//From the connection pool to get the connection, Conn is the enhanced connection conn = Datasource.getconnection (); } catch (SQLException e) {e.printstacktrace (); } return conn; /** * Release Resources * @param stmt * @param conn * * public static void release (Statement stmt,connection con N) {if (stmt! = null) {try {stmt.close (); } catch (SQLException e) {e.printstacktrace (); }} if (conn! = null) {try {//has become returned ... conn.close (); } CatCH (SQLException e) {e.printstacktrace (); }}}/** * Free resources * @param stmt * @param conn * * public static void release (ResultSet rs,st Atement Stmt,connection conn) {if (rs! = null) {try {rs.close (); } catch (SQLException e) {e.printstacktrace (); }} if (stmt! = null) {try {stmt.close (); } catch (SQLException e) {e.printstacktrace (); }} if (conn! = null) {try {//has modified the close (), it was destroyed, and now the method becomes the return connection. Conn.close (); } catch (SQLException e) {e.printstacktrace (); } } }}
6.jsp page
<table border= "1" width= "100%" > <tr> <th> serial number </th> <th> pictures </th> & Lt;th> name </th> <th> Market price </th> <th> Mall Price </th> <th> Product Date </th> </tr> <c:foreach var= "P" items= "${page.beanlist}" varstatus= "vs" > <tr align= "Center" > <td>${vs.count}</td> <td> </td> <td>${p.pname}</td> <td>${p.market_price}</td> <td>${p.shop_price}</td> <td> ; ${p.pdate}</td> </tr> </c:forEach> pagination <tr> <th colspan= "6" > Section ${Page.pagecode} page/Total ${page.totalpage} page <a href= "${PageContext.request.contextPath}/findbypage?p C=1 "> Home </a> <c:if test= "${page.pagecode > 1}" > <a href= "${PageContext.request.contextPath}/find BYPAGE?PC=${PAGE.PAGECODE-1} "> Previous </a> </c:if> begin and end values are variable, set begin and end values Logic: * If the total number of Pages <=10 page, let begin=1 end= total pages * If total pages > 10 pages, begin= current page -5, end = Current page + 4 * head overflow: If begin<1, there is a head overflow, let begin=1 end=10 * tail Overflow: If End > Total pages, let beg in= Total Pages-9 end= total pages <c:choose> <c:when test= "${page.totalpage <=" > <c:set var= "Begin" value= "1"/> <c:set var= "End" value= "${page.totalpage}"/> </c:when> <c:otherwise> <c:set var= "Begin" Value= "${Page.pagec ode-5} "/> <c:set var=" End "value=" ${Page.pagecode + 4} "/> Header overflow problem <c:if Test= "${begin < 1}" > <c:set var= "Begin" value= "1"/> <c:set var= "End" value= "/> </c:if> <c:if test=" ${End > Page.totalpage} "> <c:set var= "Begin" Value= "${page.totalpage-9}"/> <c:set var= "E nd "value=" ${page.totalpage} "/> </c:if> </c:otherwise> </ c:choose> <c:foreach var= "i" begin= "${begin}" end= "${End" > <a href= "${Pagecont Ext.request.contextPath}/findbypage?pc=${i} ">[${I}]</a> </c:forEach> <c:if test = "${Page.pagecode < Page.totalpage}" > <a href= "${PageContext.request.contextPath}/findbypage?p C=${page.pagecode + 1} "> Next </a> </c:if> <a href=" ${pageContext.request.contextPat H}/findbypage?pc=${page. Totalpage} "> Last </a> </th> </tr></table>
Javaweb paging MySQL