jdbc+servlet+jsp员工信息管理(一):员工信息分页

源码https://download.csdn.net/download/qq_39192827/10912080

如何实现分页?分页不过是在数据库中取部分的数据显示在页面上。比如一页10行数据,我们要访问第一页,那就去数据库中取排在第1到10的数据,第二页不过是取11到20。所以实现方法中分页方法即是显示(查)方式,不过是向数据库发送了带有limit关键字的sql语句,并且显示的页面需要带有第几页的参数。

此外,还需要个计算总页数的方法,用来判断页面是不是超过范围从而带来的sql语句错误

EmpDaoImol中获取总页数方法 totalpage(int size):

/**
 * 计算总页数
 * @param size 
 * 		  每页显示的行数
 */
public int totalpage(int size) {
	Connection conn=null;
	try {
		conn=DBUtil.getConnection();
		String sql="select count(*) from emp";   //查询emp表总列数,即员工总数
		PreparedStatement prep=conn.prepareStatement(sql);
		ResultSet rs=prep.executeQuery();
		int lines=-1;
		if(rs.next()){
			lines=rs.getInt(1);  //如果获取成功,rs里只有1个数
		}
        //总列数除以每页列数,若能整除即为总页数,若不能整除+1即为总列数
		int totalpage=lines%size==0?lines:(lines/size+1);  
		return totalpage;
	} catch (Exception e) {
		e.printStackTrace();
	}finally{
		DBUtil.closeConnection(conn);
	}
	return 0;
}

EmpDapImpl中分页显示方法findByPage(int page, int size):

    /**
	 * @param page
	 * 第几页
	 * @param
	 * 每页列数
         * @return emps
         * 该页所有员工集
	 */
	public List<Emp> findByPage(int page, int size) {
		Connection conn=null;
		List<Emp> emps=new ArrayList<>();
		Emp emp=null;
		try {
			conn=DBUtil.getConnection();
			String sql="select * from emp order by empno limit ?,?";
			PreparedStatement prep=conn.prepareStatement(sql);
			prep.setInt(1, (page-1)*size);  //第page页的第一列在数据库中的位置
			prep.setInt(2, size);	//第page页的最后一列在数据库中的位置
			ResultSet rs=prep.executeQuery();
			while(rs.next()){
				int empno=rs.getInt(1);
				String ename=rs.getString(2);
				String job=rs.getString(3);
				int mgr=rs.getInt(4);
				Date hiredate=rs.getDate(5);
				double sal=rs.getDouble(6);
				double comm=rs.getDouble(7);
				int deptno=rs.getInt(8);
				emp=new Emp(empno, ename, job, mgr, hiredate, sal, comm, deptno);
				emps.add(emp);   //将page页的所有员工添加到List中
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DBUtil.closeConnection(conn);
		}
		return emps; 
	}

然后就需要在相关Servlet中接受和发送总页数,当前页数以及每页几页这样的信息

EmpServlet的service方法中的部分相关代码:

if("/emp_v4/listEmp.emp".equals(uri)){
	EmpDao dao=DaoFactory.getInstance();
	String currentPage=req.getParameter("currentPage");  //获取empList.jsp(显示页面)的当前页数据
	List<Emp> emps=null;
	if(currentPage==null){  //若显示页面没有当前页的值即为第一页
		currentPage="1";
		emps = dao.findByPage(1, 10);  //获取数据库中第一页(1-10)行的数据
	}
	emps=dao.findByPage(Integer.parseInt(currentPage), 10);
	req.setAttribute("currentPage", currentPage);
	req.setAttribute("totalPage", dao.totalpage(10));
	req.setAttribute("obj", emps);
	RequestDispatcher rq=req.getRequestDispatcher("emplist.jsp");
	rq.forward(req, res); //将当前页,总页数,员工集发送给显示页并跳转
}

最后就是显示页面(empList.jsp)获取servlet组件发送的相关数据,并且如果点击了下一页或上一页把改变的数据发送回去,获取新的内容

预计效果图:

显示页empList.jsp中相关的部分代码:

<p>
	<div align="center">
	<a href="listEmp.emp">首页</a>    <!-- 首页无当前页的值,访问servlet组件 -->
	<c:choose>
		<!-- 如果不是第一页,点击了"上一页"当前页-1,再次访问servlet组件把当前页发送过去 -->
		<c:when test="${currentPage>1}"><a href="listEmp.emp?currentPage=${currentPage-1}">上一页</a></c:when>
		<!-- 如果是第一页,"上一页"没有href -->
		<c:otherwise><a> 上一页</a></c:otherwise>
	</c:choose>
	${currentPage}  <!-- 显示第几页 -->
	<c:choose>
		<!-- 下一页的思路类似 -->
		<c:when test="${currentPage<totalPage}"><a href="listEmp.emp?currentPage=${currentPage+1}">下一页</a></c:when>
		<c:otherwise><a>下一页</a></c:otherwise>
		</c:choose>
	<a href="listEmp.emp?currentPage=${totalPage}">末页</a>
	</div>					
</p>

猜你喜欢

转载自blog.csdn.net/qq_39192827/article/details/86191435