1. 程式人生 > >JSP+Servlet+oracle 實現分頁

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>