国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

[學習小筆記] JSP分頁顯示數據

2019-11-08 00:49:20
字體:
來源:轉載
供稿:網友
這幾天一直在復習jsp相關的知識,昨天重新做了一次JSP分頁顯示功能,但是因為時間原因沒能及時做好記錄,今天將這個小知識點補上。 首先我們需要一個連接數據庫的幫助類:DbConnection,一個servlet服務器類:AllGoods,一個實體類:Goods,一個操作數據庫類:SelectGoodsDB,一個JSP頁面:homePage。 工具類和實體類就不具體謝了,主要寫一下查詢和服務器的處理。

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> &nbsp;&nbsp;${page} / ${max} &nbsp;&nbsp; <a href="goods?page=<%= currentPage == max ? max : currentPage + 1%>">下一頁</a> <a href="goods?page=${max}">尾頁</a> </td> </tr> </table></body></html>

這樣,分頁查詢的功能就可以實現了,主要的代碼都在這里了,需要注意的是每次進入的時候都必須從servlet服務器類進入,不能直接從jsp頁面進入,不然就會報錯。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兰西县| 庐江县| 远安县| 确山县| 平谷区| 肇庆市| 南阳市| 海原县| 长泰县| 清水县| 清徐县| 南昌县| 海南省| 玉溪市| 泸西县| 裕民县| 印江| 黔南| 泊头市| 高尔夫| 峨边| 闵行区| 吉木萨尔县| 广汉市| 睢宁县| 盐边县| 浮山县| 吉安市| 洛扎县| 阳山县| 丰台区| 迭部县| 阿鲁科尔沁旗| 江门市| 贵定县| 万山特区| 江陵县| 武定县| 右玉县| 会宁县| 邮箱|