jsp中利用MySQL實現分頁技術

來源:互聯網
上載者:User

標籤:std   null   實現   page   href   容器   .exe   利用   base   

分頁是很常用的一種技術,而mysql中對於分頁的操作也很簡單,今天就說說如何在jsp頁面中利用標籤來最簡化的實現分頁:

連結:MySQL分頁技術詳解http://blog.csdn.net/u011637069/article/details/49928513

 

step1:編寫DAO中代碼:

 

[java] view plain copy 
  1. public List<Employee> findAll2(int page, int perPageRows) throws Exception {  
  2.     List<Employee> employees = new ArrayList<Employee>();  
  3.     Connection conn = null;  
  4.     try{  
  5.         conn = DBUtil.getConnection();  
  6.         PreparedStatement prep =   
  7.             conn.prepareStatement("select id,name,salary,age from t_emp limit ?,?");  
  8.         prep.setInt(1, (page-1)*perPageRows);  
  9.         prep.setInt(2,perPageRows);  
  10.         ResultSet rs = prep.executeQuery();  
  11.         while(rs.next()){  
  12.             int id = rs.getInt("id");  
  13.             String name = rs.getString("name");  
  14.             double salary = rs.getDouble("salary");  
  15.             int age = rs.getInt("age");  
  16.             Employee e = new Employee(id, name, salary, age);  
  17.             employees.add(e);  
  18.         }  
  19.     }catch (Exception e) {  
  20.         e.printStackTrace();  
  21.         throw e;  
  22.     }finally{  
  23.         DBUtil.close(conn);  
  24.     }  
  25.     return employees;  
  26. }  

 

 

[java] view plain copy 
  1. public int findPages(int perPageRows) throws Exception {  
  2.     int totalPage = 0;  
  3.     Connection conn = null;  
  4.     try{  
  5.         conn = DBUtil.getConnection();  
  6.         Statement state = conn.createStatement();  
  7.         ResultSet rs = state.executeQuery("select count(*) from t_emp");  
  8.         int rows = 0;  
  9.         if(rs.next()){  
  10.             rows = rs.getInt(1);  
  11.         }  
  12.         if(rows % perPageRows == 0){  
  13.             totalPage = rows / perPageRows;  
  14.         }else{  
  15.             totalPage = rows / perPageRows + 1;  
  16.         }  
  17.     }catch (Exception e) {  
  18.         e.printStackTrace();  
  19.         throw e;  
  20.     }finally{  
  21.         DBUtil.close(conn);  
  22.     }  
  23.     return totalPage;  
  24. }  

 

 

step2:編寫servlet代碼:

 

[java] view plain copy 
  1. EmployeeDAO dao = (EmployeeDAO) Factory.getInstance("EmployeeDAO");  
  2. List<Employee> employees = null;  
  3. String pageStr = request.getParameter("page");  
  4.   
  5. int perPageRows = 5;  
  6. int totalPage = 0;  
  7. int page = 1;  
  8. try {  
  9.     totalPage = dao.findPages(perPageRows);  
  10.     if(pageStr!=null){  
  11.         page = Integer.parseInt(pageStr);  
  12.     }  
  13.     if(page<1 || page>totalPage){  
  14.         page = 1;  
  15.     }  
  16.     employees = dao.findAll2(page,perPageRows);  
  17.     //轉寄  
  18.     //step1,綁定資料  
  19.     request.setAttribute("employees",employees);  
  20.     request.setAttribute("page",page);  
  21.     request.setAttribute("totalPage",totalPage);  
  22.     //step2,獲得轉寄站  
  23.     RequestDispatcher rd = request.getRequestDispatcher("/emplist.jsp");  
  24.     //step3,轉寄  
  25.     rd.forward(request, response);  
  26. } catch (Exception e1) {  
  27.     e1.printStackTrace();  
  28.     //1-轉寄處理異常  
  29.     //request.setAttribute("syserror", "系統正忙,請稍後重試!");  
  30.     //request.getRequestDispatcher("error.jsp").forward(request, response);  
  31.     //2-交給容器處理  
  32.     throw new ServletException(e1);  
  33. }  


step3:jsp代碼:

 

 

[html] view plain copy 
  1. <c:choose>  
  2.     <c:when test="${page>1}">  
  3.         <a href="list.do?page=${page-1}">上一頁</a>  
  4.     </c:when>  
  5.     <c:otherwise>  
  6.         上一頁  
  7.     </c:otherwise>  
  8. </c:choose>  
  9. 第${page}頁  
  10. <c:choose>  
  11.     <c:when test="${page<totalPage}">  
  12.         <a href="list.do?page=${page+1}">下一頁</a>  
  13.     </c:when>  
  14.     <c:otherwise>  
  15.         下一頁  
  16.     </c:otherwise>  
  17. </c:choose>  
  18. 共${totalPage}頁  

 

jsp中利用MySQL實現分頁技術

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.