package com.news.util; /** * 有了记录总条数后,页面控制类; * 根据总记录数,每页显示条数,来得到总页数; * @author Administrator * */ public class PageControler { public int getTotalPages(int count,int pageSize){ int totalPages=0; //如果总记录数除以每页显示条数,能够整除; //总页数就是它们的商,否则为商+1 if(count%pageSize==0){ totalPages=count/pageSize; }else{ totalPages=count/pageSize+1; } return totalPages; } }
UserDao中的抽象方法 //通过查询获取数据表中数据总条数; public int getCount(); //根据当前页码和页数,来分页的方法;返回User集合; public List<User>queryUserByPage(int currentPage,int pageSize); //UserDaoImpl实现类中的方法 /* 获得user的总个数,即要分页的总记录数 * @see com.news.dao.UserDao#getCount() */ @Override public int getCount() { Connection con=null; Statement st=null; ResultSet rs=null; int count=0; try { con=DaoFactory.getConnection(); st=con.createStatement(); String sql="select count(*) from t_user"; rs=st.executeQuery(sql); if(rs.next()){ count=rs.getInt(1); } } catch (SQLException e) { e.printStackTrace(); }finally{ DaoFactory.closeAll(rs, st, con); } return count; } @Override public List<User> queryUserByPage(int currentPage, int pageSize) { Connection con=null; Statement st=null; ResultSet rs=null; List<User>users=null; try { con=DaoFactory.getConnection(); st=con.createStatement(); users=new ArrayList<User>(); String sql="select top "+pageSize+" * from t_user where id not in (select top "+(currentPage-1)*pageSize+" id from t_user order by id)order by id"; rs=st.executeQuery(sql); //遍历结果集 while(rs.next()){ //从数据库中取出一条记录后,放到实体对象中; User user=new User(); user.setId(rs.getInt("id")); user.setUsername(rs.getString("username")); user.setEmail(rs.getString("email")); user.setAddress(rs.getString("address")); user.setHobby(rs.getString("hobby")); //将实体对象放入集合中; users.add(user); } } catch (SQLException e) { e.printStackTrace(); }finally{ DaoFactory.closeAll(rs, st, con); } return users; }
show_user.jsp页面代码如下
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ page import="com.news.dao.*,com.news.dao.impl.*,com.news.entity.*,com.news.util.*" %> <% 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="https://www.ctyun.cn/portal/link.html?target=%26lt%3B%25%3DbasePath%25%26gt%3B"> <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="https://www.ctyun.cn/portal/link.html?target=css%2Fcss.css"> </head> <body> <table border="1" align="center"> <caption>=用户列表</caption> <tr> <td>编号</td> <td>账号</td> <td>密码</td> <td>邮箱</td> <td>住址</td> <td>爱好</td> </tr> <% //获取当前页码,如果获取不到,默认是第一页; String pageIndex=request.getParameter("pageIndex"); if(pageIndex==null){ pageIndex="1"; } int currentPage=Integer.parseInt(pageIndex); int pageSize=4; UserDao ud=new UserDaoImpl(); PageControler pc=new PageControler(); int count=ud.getCount(); //获得总页数; int totalPages=pc.getTotalPages(count,pageSize); //分页查询,得到当前页数数据; List<User>users=ud.queryUserByPage(currentPage,pageSize); for(int i=0;i<users.size();i++){ User user=users.get(i); %> <tr> <td><%=user.getId() %></td> <td><%=user.getUsername() %></td> <td><%=user.getPwd() %></td> <td><%=user.getEmail() %></td> <td><%=user.getAddress() %></td> <td><%=user.getHobby() %></td> </tr> <% } %> <!-- 分页控制 --> <tr class="altrow"> <td colspan="6"> 第<%=currentPage %>页/共<%=totalPages %>页 <a href='https://www.ctyun.cn/portal/link.html?target=admin%2Fshow_user.jsp%3FpageIndex%3D%26lt%3B%25%3D1+%25%26gt%3B'>首页</a> <% if(currentPage>1){ %> <a href='https://www.ctyun.cn/portal/link.html?target=admin%2Fshow_user.jsp%3FpageIndex%3D%26lt%3B%25%3DcurrentPage-1+%25%26gt%3B'>上一页||</a> <% } if(currentPage<totalPages){ %> <a href='https://www.ctyun.cn/portal/link.html?target=admin%2Fshow_user.jsp%3FpageIndex%3D%26lt%3B%25%3DcurrentPage%2B1+%25%26gt%3B'>下一页</a>|| <%} %> <a href='https://www.ctyun.cn/portal/link.html?target=admin%2Fshow_user.jsp%3FpageIndex%3D%26lt%3B%25%3DtotalPages+%25%26gt%3B'>末页</a> </td> </tr> </table> </body> </html>