分頁操作
阿新 • • 發佈:2017-08-18
from meta itl highlight exce runner page sheet 查詢
進行網頁開發,那麽就少不了分頁查詢。分頁查詢在mysql中是最方便的,使用mysql的關鍵字LIMIT(mysql獨有),即可進行分頁查詢。如下是mysql的分頁查詢的sql語句:
SELECT * FROM hero LIMIT ?, ? # 第一個?表示的是從那條數據開始查詢 # 第二個?表示的是每次查詢的數據的條數
而在Oracle中也有一個方言,專門用來Oracle數據庫的分頁查詢,關鍵字為:ROWNUM 和 row_number()
下面使用mysql演示分頁:
jsp頁面:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>分頁操作</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 href="table.css" rel="stylesheet" type="text/css"> </head> <body> <div align="center"><h1 style="color: #000000; align:center">演示分頁查詢</h1></div> <table cellpadding="1" border="1" cellspacing="0" bordercolor="black" align="center"> <tr class="tab-2"> <td>編號</td> <td>英雄名字</td> <td>英雄Job</td> <td>英雄上司編號</td> <td>英雄雇傭日期</td> <td>英雄薪水</td> <td>英雄獎金</td> <td>英雄類型</td> </tr> <%-- <c:if test="${null ne heros }"> --%> <c:forEach items="${heros }" var="hero"> <tr class="tab-5"> <td>${hero.herono }</td> <td>${hero.heroname }</td> <td>${hero.herojob }</td> <td>${hero.heromgr }</td> <td>${hero.herohiredate }</td> <td>${hero.herosal }</td> <td>${hero.herocomm }</td> <td>${hero.herotype }</td> </tr> </c:forEach> <%-- </c:if> --%> </table><br/><br/> <div align="center"> <c:if test="${num-1 > 0 }"> <a href="<c:url value=‘/SplitServlet‘/>?method=splitenum&num=${num-1}">上一頁</a> <a href="<c:url value=‘/SplitServlet‘/>?method=splitenum&num=${num-1}">第${num-1 }頁</a> </c:if> <a href="<c:url value=‘/SplitServlet‘/>?method=splitenum&num=${num}">第${num }頁</a> <c:if test="${num+1 <= all }"> <a href="<c:url value=‘/SplitServlet‘/>?method=splitenum&num=${num+1}">第${num+1 }頁</a> <a href="<c:url value=‘/SplitServlet‘/>?method=splitenum&num=${num+1}">下一頁</a> </c:if> <a href="<c:url value=‘/SplitServlet‘/>?method=splitenum&num=1">刷新</a> <font color="red" size="5px">總共 ${all } 頁</font> </div> </body> </html>
dao:
package cn.geore.splitpage; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import priv.geore.toolutils.jdbc.FirmQueRunner; public class SplitPageDao { QueryRunner runner = new FirmQueRunner(); /** * 查詢每頁的數據 * @param page * @param count * @return * @throws SQLException */ public List<Hero> findHero(int page, int count) throws SQLException { String sql = "SELECT * FROM hero LIMIT ?,?"; Object[] params = {(page-1)*4,count}; return (List<Hero>) runner.query(sql, new BeanListHandler<Hero>(Hero.class), params); } /** * 查詢頁數 * @return * @throws SQLException */ public Number count() throws SQLException { String sql = "SELECT count(*) FROM hero"; return runner.query(sql, new ScalarHandler<Number>()); } }
servlet:
package cn.geore.splitpage; import java.io.IOException; import java.sql.SQLException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import priv.geore.toolutils.web.FirmHttpServlet; public class SplitServlet extends FirmHttpServlet { SplitPageDao dao = new SplitPageDao(); /** * 分頁查找數據 * @param request * @param response * @return * @throws ServletException * @throws IOException */ public String splitenum(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int num = Integer.valueOf(request.getParameter("num")); List<Hero> heros = null; int all = 0; try { heros = dao.findHero(num, 4); all = (int) Math.floor(dao.count().intValue() / 4.0); } catch (SQLException e) { throw new RuntimeException(e); } request.setAttribute("heros", heros); request.setAttribute("num", num); request.setAttribute("all", all); return "f:/index.jsp"; } }
運行截圖:
分頁操作