SelectGoodsDB類:
public class SelectGoodsDB { // 鏈接數據庫對象 PRivate Connection conn = null; // 執行語句對象 private PreparedStatement ps = null; // 接受返回的結果集對象 private ResultSet rs = null; // 每頁顯示商品的數量 private final int NUM = 3; List<Goods> list; // 查詢所有的商品信息 public List<Goods> getGoods(int page) { list = new ArrayList<Goods>(); // 獲取數據庫的鏈接 conn = DbConnection.getConnection(); // 定義查詢語句(查詢語句需要用limit來查詢每個頁面的數據) String sql = "SELECT `no`, `name`, address, time, type FROM t_goods LIMIT " + (page - 1) * NUM + ", " + NUM + ";"; try { // 執行查詢語句 ps = conn.prepareStatement(sql); // 接受返回的結果集 rs = ps.executeQuery(); // 循環遍歷結果,保存到集合當中 while (rs.next()) { Goods good = new Goods(); good.setG_no(rs.getString("no")); good.setG_name(rs.getString("name")); good.setG_address(rs.getString("address")); good.setG_time(rs.getString("time")); good.setG_type(rs.getString("type")); list.add(good); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DbConnection.closeAll(rs, ps, conn); } return list; } // 查詢計算最大頁碼數 public int getNums() { int row = 0; // 獲取數據庫的鏈接 conn = DbConnection.getConnection(); // 定義查詢語句 String sql = "SELECT COUNT(no) FROM t_goods;"; try { // 操作對象 ps = conn.prepareStatement(sql); // 執行sql語句 rs = ps.executeQuery(); // 保存商品總數 if (rs.next()) row = rs.getInt(1); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DbConnection.closeAll(rs, ps, conn); } // 最后這里我們需要對查詢到的數據進行一下判斷,用三元運算符判斷出具體的頁碼是多少并返回 return row % NUM != 0 ? row / NUM + 1 : row / NUM; }}AllGoods類:
public class AllGoods extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // 設置編碼格式處理亂碼問題 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); // 保存需要查詢的頁碼 int page = 0; // 用try catch捕獲異常,當發生異常時說明是第一次進入到首頁,默認頁碼為1 try { page = Integer.valueOf(request.getParameter("page")); } catch (Exception e) { // TODO: handle exception page = 1; } // 通過頁碼查詢到對應頁面顯示的數據 List<Goods> list = new SelectGoodsDB().getGoods(page); // 查詢最大頁碼 int max = new SelectGoodsDB().getNums(); request.setAttribute("goods", list); request.setAttribute("page", page); request.setAttribute("max", max); request.getRequestDispatcher("homePage.jsp").forward(request, response); }}homePage.jsp:
<%@page import="java.util.ArrayList"%><%@page import="com.galibaba.entity.Goods"%><%@page import="java.util.List"%><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>首頁</title></head><style> tr { height: 40px; text-align: center; } th, td { border-bottom: 1px solid skyblue; } .fanye { color: blue; text-decoration: underline; cursor: pointer; }</style><body><% // 保存傳遞過來當前頁面的數據 List<Goods> list = (ArrayList<Goods>) request.getAttribute("goods");%><h1 style="text-align: center">商品信息顯示</h1> <table align="center" width="600px" cellpadding="0" cellspacing="0"> <tr> <th>序號</th> <th>姓名</th> <th>時間</th> <th>地址</th> <th>類型</th> </tr> <% for(Goods g : list) { %> <tr> <td><%= g.getG_no() %></td> <td><%= g.getG_name() %></td> <td><%= g.getG_time() %></td> <td><%= g.getG_address() %></td> <td><%= g.getG_type() %></td> </tr> <% } // 獲取到當前顯示的頁碼 int currentPage = Integer.valueOf(request.getAttribute("page").toString()); // 獲取到最大頁碼 int max = Integer.valueOf(request.getAttribute("max").toString()); %> <tr> <td colspan="5"> <a href="goods?page=1">首頁</a> <a href="goods?page=<%= currentPage == 1 ? 1 : currentPage - 1%>">上一頁</a> ${page} / ${max} <a href="goods?page=<%= currentPage == max ? max : currentPage + 1%>">下一頁</a> <a href="goods?page=${max}">尾頁</a> </td> </tr> </table></body></html>這樣,分頁查詢的功能就可以實現了,主要的代碼都在這里了,需要注意的是每次進入的時候都必須從servlet服務器類進入,不能直接從jsp頁面進入,不然就會報錯。
新聞熱點
疑難解答