JSP+Servlet+oracle 实现分页

效果如下图:

分析:

1、oracle分页语句的实现select * from (select rownum as rn,t.* from students t where rownum<=5) where rn>0;

2、需要构造分页对象,包括

3、当前页面不为1的时候显示 首页|上一页,当前页面不为总的页数的时候显示 下一页|尾页

      <c:foreach>begin=1 end=总的页数  设置 1 2 3 页;

代码:

servlet:

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        int currentPage = Integer.parseInt(request.getParameter("currentPage"));
        StudentService service = new StudentServiceImpl();
        try {
            PageVO vo = service.findStudentPage(currentPage);
            request.setAttribute("pageVO", vo);
            
            request.getRequestDispatcher("stu_page_list.jsp").forward(request, response);;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        
    }

serviceImpl:

  

  public PageVO findStudentPage(int currentPage) throws SQLException {
        StudentDao dao = new StudentDaoImpl();
        List<Student> list = dao.findStudentPage(currentPage);
        PageVO vo = new PageVO();
        vo.setCurrentPage(currentPage);
        vo.setList(list);
        vo.setPageCount(StudentService.PAGE_NUM);
        
        //获取总记录数据
        int count = dao.getCount();
        vo.setCount(count);
        //总记录/每页条数  得到总页数
        vo.setTotlePage(count%StudentService.PAGE_NUM==0?count/StudentService.PAGE_NUM:count/StudentService.PAGE_NUM+1);
        
        return vo;
    }

daoImpl:

    @Override
    public List<Student> findStudentPage(int currentPage) throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
        return qr.query("select * from (select rownum as rn,t.* from students t where rownum<=?) where rn>=?", 
                new BeanListHandler<Student>(Student.class), StudentService.PAGE_NUM*currentPage,(currentPage-1)*StudentService.PAGE_NUM+1);
    }

    @Override
    public int getCount() throws SQLException {
        QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
        BigDecimal bg = (BigDecimal) qr.query("select count(*) from students", new ScalarHandler());
        return bg.intValue();
    }

jsp:

<form action="FindStudentByConditionServlet" method="post">
		
	<table border="1" width="700">
		<tr >
			<td colspan="8">
				
				按姓名查询:<input type="text" name="name" value="${requestScope.name }"/>
				&nbsp;
				按性别查询:<select name="sex">
							<option value="">--请选择--
							<option value="男">男
							<option value="女">女
						  </select>
				&nbsp;&nbsp;&nbsp;
				<input type="submit" value="查询">
				&nbsp;&nbsp;&nbsp;
				<a href="add.jsp">添加</a>
			</td>
		</tr>
		<tr align="center">
			<td>编号</td>
			<td>简介</td>
			<td>姓名</td>
			<td>性别</td>
			<td>出生日期</td>
			<td>专业</td>
			<td>爱好</td>
			<td>操作</td>
		</tr>
		
		<c:forEach items="${requestScope.pageVO.list }" var="stu"> 
			<tr align="center">
			<td>${stu.student_id }</td>
			<td>${stu.info }</td>
			<td>${stu.name }</td>
			<td>${stu.sex }</td>
			<td>${stu.dob }</td>
			<td>${stu.specialty }</td>
			<td>${stu.hobby }</td>
			<td><a href="EditServlet?student_id=${stu.student_id }">修改</a>
				<a href="#" onclick="confirmDel(${stu.student_id })">删除</a>
			</td> 
		</tr>
		</c:forEach>
		
		<tr>
			<td colspan="8">
				第${pageVO.currentPage }/${pageVO.totlePage }&nbsp;&nbsp;
				每页显示${pageVO.pageCount }条&nbsp;&nbsp;
				总记录数${pageVO.count }&nbsp;&nbsp;
				
				<c:if test="${pageVO.currentPage!=1 }">
					<a href="StudentListPageServlet?currentPage=1">首页</a>
					|
					<a href="StudentListPageServlet?currentPage=${pageVO.currentPage-1 }">上一页</a>
				</c:if>
				
				<c:forEach begin="1" end="${pageVO.totlePage }" var="i">
					<c:if test="${i==pageVO.currentPage }">
						${i }&nbsp;&nbsp;	
					</c:if>
					<c:if test="${i!=pageVO.currentPage }">
						<a href="StudentListPageServlet?currentPage=${i }">${i }</a>&nbsp;&nbsp;
					</c:if>
				</c:forEach>
				
				<c:if test="${pageVO.currentPage!=pageVO.totlePage }">
					<a href="StudentListPageServlet?currentPage=${pageVO.currentPage+1 }">下一页</a>
					|
					<a href="StudentListPageServlet?currentPage=${pageVO.totlePage }">尾页</a>
				</c:if>
			
			</td>
		</tr>
	</table>
	
	</form>

猜你喜欢

转载自blog.csdn.net/xldmx/article/details/84642303
今日推荐