tab page class record information
. 1 public class DBPage <T> { 2 // total number of pages . 3 Private int totalPageCount =. 1 ; . 4 // page size, i.e., number of records per page . 5 Private int the pageSize =. 3 ; . 6 // total number of records . 7 Private int The totalCount = 0 ; 8 // current page . 9 Private int currPageNo =. 1 ; 10 // page set . 11 List <T> = usersList new new the ArrayList <> (); 12 is public int getTotalPageCount() { 13 return totalPageCount; 14 } 15 public void setTotalPageCount(int totalPageCount) { 16 this.totalPageCount = totalPageCount; 17 } 18 public int getPageSize() { 19 return pageSize; 20 } 21 public void setPageSize(int pageSize) { 22 this.pageSize = pageSize; 23 } 24 public int given otal count () { 25 return total count; 26 } 27 public void seen otal count ( int total count) { 28 this .totalCount = total count; 29 } 30 public int getCurrPageNo () { 31 return currPageNo; 32 } 33 public void setCurrPageNo ( int currPageNo) { 34 this .currPageNo = currPageNo; 35 } 36 Public List <T> getUsersList() { 37 return usersList; 38 } 39 public void setUsersList(List<T> usersList) { 40 this.usersList = usersList; 41 } 42 }
Paging a user operation information acquisition method of the class
1 public DBPage<User> queryByPage(DBPage<User> dbPage) { 2 CachedRowSet totalCrs = this.execQuery("select count(1) from t_user"); 3 try { 4 while (totalCrs.next()) { 5 int count = totalCrs.getInt(1); 6 dbPage.setTotalCount(count); 7 } 8 } catch (SQLException e) { 9 e.printStackTrace(); 10 } finally { 11 try { 12 totalCrs.close(); 13 } catch (SQLException e) { 14 e.printStackTrace(); 15 } 16 } 17 18 if (dbPage.getTotalCount() > 0) { 19 // 计算总页数 20 int totalCount = dbPage.getTotalCount(); 21 int pageSize = dbPage.getPageSize(); 22 dbPage.setTotalPageCount(totalCount % pageSize == 0 ? (totalCount / pageSize) : totalCount / pageSize + 1); 23 } 24 25 CachedRowSet pageCrs = this.execQuery("select * from t_user limit ?,?", 26 (dbPage.getCurrPageNo() - 1) * dbPage.getPageSize(), dbPage.getPageSize()); 27 try { 28 while (pageCrs.next()) { 29 String uname = pageCrs.getString("uname"); 30 String upwd = pageCrs.getString("upwd"); 31 int isAdmin = pageCrs.getInt("isAdmin"); 32 dbPage.getUsersList().add(new User(0, uname, upwd, isAdmin)); 33 } 34 } catch (SQLException e) { 35 e.printStackTrace(); 36 } finally { 37 try { 38 pageCrs.close(); 39 } catch (SQLException e) { 40 e.printStackTrace(); 41 } 42 } 43 return dbPage; 44 }
jsp page display pagination information
1 <% 2 DBPage<User> dbPage = new DBPage<>(); 3 int pageNo; 4 String dbPageNo = (String) request.getParameter("pageNo"); 5 if (dbPageNo == null) { 6 pageNo = 1; 7 } else { 8 pageNo = Integer.valueOf(dbPageNo); 9 dbPage.setCurrPageNo(pageNo); 10 } 11 UserDaoImpl userDaoImpl = new UserDaoImpl(); 12 userDaoImpl.queryByPage(dbPage); 13 %> 14 <table border="1" align="center"> 15 <tr> 16 <td>用户名</td> 17 <td>密码</td> 18 <td>管理员</td> 19 </tr> 20 <% 21 for (User u : dbPage.getUsersList()) { 22 %> 23 <tr> 24 <td><%=u.getUname()%></td> 25 <td><%=u.getPwd()%></td> 26 <td><%=u.getIsAdmin()%></td> 27 </tr> 28 <% 29 } 30 %> 31 </table> 32 当前<%=pageNo%>页 33 <br /> 34 <% 35 36 %> 37 <a href="?pageNo=<%=1%>">首页</a> 38 <a href="?pageNo=<%=pageNo-1<1?pageNo:pageNo-1%>">上一页</a> 39 <a href="?pageNo=<%=(pageNo+1)>dbPage.getTotalPageCount()?pageNo:pageNo+1%>">下一页</a> 40 <a href="?pageNo=<%=dbPage.getTotalPageCount()%>">尾页</a>