jsp+mysql實(shí)現(xiàn)網(wǎng)頁(yè)的分頁(yè)查詢(xún)
本文實(shí)例為大家分享了jsp+mysql實(shí)現(xiàn)網(wǎng)頁(yè)的分頁(yè)查詢(xún)的具體代碼,供大家參考,具體內(nèi)容如下
一、實(shí)現(xiàn)分頁(yè)查詢(xún)的核心sql語(yǔ)句
(1)查詢(xún)數(shù)據(jù)庫(kù)的記錄總數(shù)的sql語(yǔ)句:
select count(*) from +(表名);
(2)每次查詢(xún)的記錄數(shù)的sql語(yǔ)句:
其中:0是搜索的索引,2是每次查找的條數(shù)。
select * from 表名 limit 0,2;
二、代碼實(shí)現(xiàn)
*上篇寫(xiě)過(guò)這兩個(gè)類(lèi) , DBconnection類(lèi):用于獲取數(shù)據(jù)庫(kù)連接,Author對(duì)象類(lèi)。這兩個(gè)類(lèi)的代碼點(diǎn)擊連接查看。點(diǎn)擊鏈接查看 DBconnection類(lèi)和Author對(duì)象類(lèi)
(1)登錄頁(yè)面:index.jsp。
<%@ page language="java" contentType="text/html; charset=utf-8" ? ? pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> ? ? <a href="AuthorListPageServlet">用戶(hù)列表分頁(yè)查詢(xún)</a> </body> </html>
(2)顯示頁(yè)面:userlistpage.jsp。
<%@ page language="java" contentType="text/html; charset=utf-8" ? ? pageEncoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>查詢(xún)頁(yè)面</title> </head> <body> <table border="1"> ? <tr> ? ? <td>編號(hào)</td> ? ? <td>名稱(chēng)</td> ? ? <td>價(jià)格</td> ? ? <td>數(shù)量</td> ? ? <td>日期</td> ? ? <td>風(fēng)格</td> ? </tr> ? <c:forEach items="${pageBean.list}" var="author"> ? <tr> ? ? <td>${author.id}</td> ? ? <td>${author.name }</td> ? ? <td>${author.price }</td> ? ? <td>${author.num }</td> ? ? <td>${author.dates}</td> ? ? <td>${author.style}</td> ? </tr> ? </c:forEach> </table> <c:if test="${ pageBean.record>0}"> <div> ? ? ?? ? ? ? <c:if test="${pageBean.currentPage <= 1}"> ? ? ? <span>首頁(yè)</span> ? ? ? <span>上一頁(yè)</span> ? ? ? <a href ="AuthorListPageServlet?currPage=${pageBean.currentPage + 1 }">下一頁(yè)</a> ? ? ? <a href ="AuthorListPageServlet?currPage=${pageBean.totalPage }">尾頁(yè)</a> ? ? ? </c:if> ? ? ?? ? ? ? <c:if test="${pageBean.currentPage > 1 && pageBean.currentPage < pageBean.totalPage ?}"> ? ? ? ?<a href ="AuthorListPageServlet?currPage=1">首頁(yè)</a> ? ? ? <a href ="AuthorListPageServlet?currPage=${pageBean.currentPage - 1 }">上一頁(yè)</a> ? ? ? <a href ="AuthorListPageServlet?currPage=${pageBean.currentPage + 1 }">下一頁(yè)</a> ? ? ? <a href ="AuthorListPageServlet?currPage=${pageBean.totalPage }">尾頁(yè)</a> ? ? ? </c:if> ? ? ? ? ? ?<c:if test="${ pageBean.currentPage >= pageBean.totalPage}"> ? ? ? <a href ="AuthorListPageServlet?currPage=1">首頁(yè)</a> ? ? ? <a href ="AuthorListPageServlet?currPage=${pageBean.currentPage - 1 }">上一頁(yè)</a> ? ? ?<span>下一頁(yè)</span> ? ? ?<span>尾頁(yè)</span> ? ? ?</c:if> </div> </c:if> </body> </html>
(3)功能實(shí)現(xiàn):AuthorDao.java。
package com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.entity.Author; public class AuthorDao { ?? ? ?? ? public ?Author check(String username ,int ?password ) { ?? ??? ?? ?? ??? ? Author obj = null ; ?? ??? ? try { ?? ??? ??? ?DBConnection db = new DBConnection(); ?? ??? ??? ?//獲取數(shù)據(jù)庫(kù)連接 ?? ??? ??? ?Connection conn = db.getConn(); ?? ??? ??? ? ?? ??? ??? ?String sql="select *from furnitures where name = ? and id = ?"; ?? ??? ??? ? ?? ??? ??? ?PreparedStatement ps=conn.prepareStatement(sql); ?? ??? ??? ?//設(shè)置用戶(hù)名和密碼作為參數(shù)放入sql語(yǔ)句 ?? ??? ??? ?ps.setString(1,username); ?? ??? ??? ?ps.setInt(2,password); ?? ??? ??? ?//執(zhí)行查詢(xún)語(yǔ)句 ?? ??? ??? ?ResultSet rs = ps.executeQuery(); ?? ??? ??? ?//用戶(hù)名和密碼正確,查到數(shù)據(jù) ?歐式風(fēng)格 ?茶幾 ?? ??? ??? ?if(rs.next()) { ?? ??? ??? ??? ?obj = new Author(); ?? ??? ??? ??? ?obj.setId(rs.getInt(1)); ?? ??? ??? ??? ?obj.setName(rs.getString(2)); ?? ??? ??? ??? ?obj.setPrice(rs.getInt(3)); ?? ??? ??? ??? ?obj.setNum(rs.getInt(4)); ?? ??? ??? ??? ?obj.setDates(rs.getString(5)); ?? ??? ??? ??? ?obj.setStyle(rs.getString(6)); ?? ??? ??? ?} ?? ??? ?} catch (SQLException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ??? ? return obj; ?? ? } ?? ? /** ?? ? ?* 用戶(hù)列表信息查詢(xún) ?? ? ?* @return ?? ? ?*/ ?? ? public List<Author> queryAuthorList(){ ?? ??? ? Author obj = null ; ?? ??? ? List<Author> list = new ArrayList<Author>(); ?? ??? ? try { ?? ??? ??? ?DBConnection db = new DBConnection(); ?? ??? ??? ?//獲取數(shù)據(jù)庫(kù)連接 ?? ??? ??? ?Connection conn = db.getConn(); ?? ??? ??? ? ?? ??? ??? ?String sql="select *from furnitures"; ?? ??? ??? ? ?? ??? ??? ?PreparedStatement ps=conn.prepareStatement(sql); ?? ? ?? ??? ??? ?//執(zhí)行查詢(xún)語(yǔ)句 ?? ??? ??? ?ResultSet rs = ps.executeQuery(); ?? ??? ??? ?//用戶(hù)名和密碼正確,查到數(shù)據(jù) ?歐式風(fēng)格 ?茶幾 ?? ??? ??? ?//循環(huán)遍歷獲取用戶(hù)信息 ?? ??? ??? ?while(rs.next()) { ?? ??? ??? ??? ? ?? ??? ??? ??? ?obj = new Author(); ?? ??? ??? ??? ?obj.setId(rs.getInt(1)); ?? ??? ??? ??? ?obj.setName(rs.getString(2)); ?? ??? ??? ??? ?obj.setPrice(rs.getInt(3)); ?? ??? ??? ??? ?obj.setNum(rs.getInt(4)); ?? ??? ??? ??? ?obj.setDates(rs.getString(5)); ?? ??? ??? ??? ?obj.setStyle(rs.getString(6)); ?? ??? ??? ??? ?//將對(duì)象加入list里邊 ?? ??? ??? ??? ?list.add(obj); ?? ??? ??? ?} ?? ??? ?} catch (SQLException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ??? ? return list; ?? ? } ?? ?? ?? ?? ?? ? /** ?? ? ?* 查詢(xún)用戶(hù)表總記錄數(shù) ?? ? ?* @return ?? ? ?*/ ?? ? public int queryUserListCount() { ?? ??? ? DBConnection db; ?? ??? ?try { ?? ??? ? ? ? db = new DBConnection(); ?? ??? ??? ? Connection conn = db.getConn(); ?? ??? ??? ? String sql = "select count(*) from furnitures"; ?? ??? ??? ?? ?? ??? ??? ? PreparedStatement ps = conn.prepareStatement(sql); ?? ??? ??? ? ResultSet rs = ps.executeQuery(); ?? ??? ??? ?? ?? ??? ??? ?? ?? ??? ??? ? if(rs.next()) { ?? ??? ??? ??? ? return rs.getInt(1); ?? ??? ??? ? } ?? ??? ??? ?? ?? ??? ??? ?? ?? ??? ?} catch (SQLException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ??? ? ?? ??? ? return 0; ?? ? } ?? ? /** ?? ? ?* 查詢(xún)用戶(hù)分頁(yè)數(shù)據(jù) ?? ? ?* @param pageIndex數(shù)據(jù)起始索引 ?? ? ?* @param pageSize每頁(yè)顯示條數(shù) ?? ? ?* @return ?? ? ?*/ ?? ? public List<Author>queryUserListPage(int pageIndex,int pageSize){ ?? ??? ?? ?? ??? ? Author obj = null; ?? ??? ? List<Author> list = new ArrayList<Author>(); ?? ??? ?? ?? ??? ? try { ?? ??? ??? ?Connection conn = new DBConnection().getConn(); ?? ??? ??? ?String sql = "select * from furnitures limit ?,?;"; ?? ??? ??? ?PreparedStatement ps = conn.prepareStatement(sql); ?? ??? ??? ?ps.setObject(1, pageIndex); ?? ??? ??? ?ps.setObject(2,pageSize); ?? ??? ??? ? ?? ??? ??? ?ResultSet rs = ps.executeQuery(); ?? ??? ??? ?//遍歷結(jié)果集獲取用戶(hù)列表數(shù)據(jù) ?? ??? ??? ? ?? ??? ??? ?while(rs.next()) { ?? ??? ??? ??? ?obj = new Author(); ?? ??? ??? ??? ? ?? ??? ??? ??? ?obj.setId(rs.getInt(1)); ?? ??? ??? ??? ?obj.setName(rs.getString(2)); ?? ??? ??? ??? ?obj.setPrice(rs.getInt(3)); ?? ??? ??? ??? ?obj.setNum(rs.getInt(4)); ?? ??? ??? ??? ?obj.setDates(rs.getString(5)); ?? ??? ??? ??? ?obj.setStyle(rs.getString(6)); ?? ??? ??? ??? ? ?? ??? ??? ??? ?list.add(obj); ?? ??? ??? ?} ?? ??? ?} catch (SQLException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ??? ? return list; ?? ? } ?? ? /** ?? ? ?* 用戶(hù)新增 ?? ? ?* @param obj ?? ? ?*/ ?? ? public void add(Author obj) { ?? ??? ? ?? ??? ?try { ?? ??? ??? ? ?? ??? ??? ?DBConnection db = new DBConnection(); ?? ??? ??? ?//獲取數(shù)據(jù)庫(kù)連接 ?? ??? ??? ?Connection conn = db.getConn(); ?? ??? ??? ? ?? ??? ??? ?String sql="insert into furnitures values(id,?,?,?,?,?)"; ?? ??? ??? ? ?? ??? ??? ?PreparedStatement ps=conn.prepareStatement(sql); ?? ??? ??? ?ps.setObject(1, obj.getName()); ?? ??? ??? ?ps.setObject(2, obj.getPrice()); ?? ??? ??? ?ps.setObject(3, obj.getNum()); ?? ??? ??? ?ps.setObject(4,obj.getDates()); ?? ??? ??? ?ps.setObject(5, obj.getStyle()); ?? ??? ??? ? ?? ??? ??? ?//執(zhí)行sql語(yǔ)句 ?? ??? ? ? ps.execute(); ?? ??? ? ?? ?? ??? ??? ? ?? ??? ?} catch (SQLException e) { ?? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ??? ??? ? ?? ? } ?? ? //刪除用戶(hù) ?? ? public void del(int id) { ?? ??? ? try { ?? ??? ??? ??? ? ?? ??? ??? ??? ?DBConnection db = new DBConnection(); ?? ??? ??? ??? ?//獲取數(shù)據(jù)庫(kù)連接 ?? ??? ??? ??? ?Connection conn = db.getConn(); ?? ??? ??? ??? ? ?? ??? ??? ??? ?String sql="delete from furnitures where id = ?"; ?? ??? ??? ??? ? ?? ??? ??? ??? ?PreparedStatement ps=conn.prepareStatement(sql); ?? ??? ??? ??? ? ?? ??? ??? ??? ?ps.setObject(1, id); ?? ??? ??? ??? ? ?? ??? ??? ??? ?//執(zhí)行sql語(yǔ)句 ?? ??? ??? ? ? ps.execute(); ?? ??? ??? ? ?? ?? ??? ??? ??? ? ?? ??? ??? ?} catch (SQLException e) { ?? ??? ??? ??? ?// TODO Auto-generated catch block ?? ??? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?} ?? ??? ??? ??? ? ?? ? } ?? ? }
(4)交互層:AuthorListPageServlet.java。
package com.servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.dao.AuthorDao; import com.entity.Author; import com.util.PageBean; /** ?* Servlet implementation class AuthorListPageServlet ?*/ @WebServlet("/AuthorListPageServlet") public class AuthorListPageServlet extends HttpServlet { ?? ?private static final long serialVersionUID = 1L; ? ? ? ? ? ? /** ? ? ?* @see HttpServlet#HttpServlet() ? ? ?*/ ? ? public AuthorListPageServlet() { ? ? ? ? super(); ? ? ? ? // TODO Auto-generated constructor stub ? ? } ?? ?/** ?? ? * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) ?? ? */ ?? ?protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?int pageSize = 2; ?? ??? ?AuthorDao ad = new AuthorDao(); ?? ??? ?//總記錄數(shù) ?? ??? ?int record = ad.queryUserListCount(); ?? ??? ?//接收頁(yè)面?zhèn)魅氲捻?yè)碼 ?? ??? ?String strPage = request.getParameter("currPage"); ?? ??? ?int currPage = 1;//默認(rèn)第一頁(yè) ?? ??? ?if(strPage != null) { ?? ??? ??? ?currPage = Integer.parseInt(strPage); ?? ? ?? ??? ?} ?? ??? ? ?? ??? ?PageBean<Author> pb = new PageBean<Author>(currPage,pageSize,record); ?? ??? ?//查詢(xún)某一頁(yè)的結(jié)果集 ?? ??? ?List<Author> list = ad.queryUserListPage(pb.getPageIndex(), pageSize); ?? ??? ?pb.setList(list); ?? ??? ?request.setAttribute("pageBean", pb); ?? ??? ?request.getRequestDispatcher("userlistpage.jsp").forward(request, response); ?? ?} ?? ?/** ?? ? * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) ?? ? */ ?? ?protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ?? ??? ?// TODO Auto-generated method stub ?? ??? ?doGet(request, response); ?? ?} }
(5)工具類(lèi):PageBean.java。作用是:獲取結(jié)果集。
package com.util; import java.util.List; public class PageBean<T>{ ?? ?private int currentPage;//當(dāng)前頁(yè)碼 ?? ?private int pageIndex;//數(shù)據(jù)起始索引 ?? ?private int pageSize;//每頁(yè)條數(shù) ?? ? ?? ? ?? ?private int record;//總記錄數(shù) ?? ?private int totalPage;//總頁(yè)數(shù) ?? ? ?? ?private List<T>list;//每頁(yè)顯示的結(jié)果集 ?? ?/** ?? ? * 構(gòu)造方法初始化pageIndex和totalPage ?? ? * @param currentPage ?? ? * @param pageIndex ?? ? * @param pageSize ?? ? */ ?? ?public PageBean(int currentPage,int pageSize,int record) { ?? ??? ? ?? ??? ?this.currentPage = currentPage; ?? ??? ?this.pageSize = pageSize; ?? ??? ?this.record = record; ?? ??? ? ?? ??? ?//總頁(yè)數(shù) ?? ??? ?if(record % pageSize == 0) { ?? ??? ??? ?//整除,沒(méi)有多余的頁(yè) ?? ??? ??? ?this.totalPage = record / pageSize; ?? ??? ??? ? ?? ??? ?} ?? ??? ?else { ?? ??? ??? ?//有多余的數(shù)據(jù),在增加一頁(yè) ?? ??? ??? ?this.totalPage = record / pageSize + 1; ?? ??? ?} ?? ??? ? ?? ??? ?//計(jì)算數(shù)據(jù)起始索引pageIndex ?? ??? ?if(currentPage < 1) { ?? ??? ??? ?this.currentPage = 1; ?? ??? ?} ?? ??? ?else if(currentPage > this.totalPage) { ?? ??? ??? ?this.currentPage = this.totalPage; ?? ??? ?} ?? ??? ?this.pageIndex = (this.currentPage -1)*this.pageSize; ?? ?} ?? ? ?? ?public int getCurrentPage() { ?? ??? ?return currentPage; ?? ?} ?? ?public void setCurrentPage(int currentPage) { ?? ??? ?this.currentPage = currentPage; ?? ?} ?? ?public int getPageIndex() { ?? ??? ?return pageIndex; ?? ?} ?? ?public void setPageIndex(int pageIndex) { ?? ??? ?this.pageIndex = pageIndex; ?? ?} ?? ?public int getPageSize() { ?? ??? ?return pageSize; ?? ?} ?? ?public void setPageSize(int pageSize) { ?? ??? ?this.pageSize = pageSize; ?? ?} ?? ?public int getRecord() { ?? ??? ?return record; ?? ?} ?? ?public void setRecord(int record) { ?? ??? ?this.record = record; ?? ?} ?? ?public int getTotalPage() { ?? ??? ?return totalPage; ?? ?} ?? ?public void setTotalPage(int totalPage) { ?? ??? ?this.totalPage = totalPage; ?? ?} ?? ?public List<T> getList() { ?? ??? ?return list; ?? ?} ?? ?public void setList(List<T> list) { ?? ??? ?this.list = list; ?? ?} ?? ? }
三、運(yùn)行結(jié)果
(1)首頁(yè):
(2)中間頁(yè):
(3)尾頁(yè):
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Jsp中解決session過(guò)期跳轉(zhuǎn)到登陸頁(yè)面并跳出iframe框架的方法
這里我們是介紹一個(gè)網(wǎng)站管理后臺(tái)三個(gè)框架頁(yè)面當(dāng)我們的jsp定義的session變量超時(shí)時(shí)用戶(hù)點(diǎn)擊時(shí)自動(dòng)退出框架頁(yè)面并跳到登錄頁(yè)面去了,下面我來(lái)給大家演示一個(gè)實(shí)例2013-08-08JSP實(shí)現(xiàn)計(jì)算器功能(網(wǎng)頁(yè)版)
這篇文章講述了JSP實(shí)現(xiàn)計(jì)算器功能的詳細(xì)代碼,網(wǎng)頁(yè)版的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-12-12Jsp+Servlet實(shí)現(xiàn)文件上傳下載 文件列表展示(二)
這篇文章主要為大家詳細(xì)介紹了Jsp+Servlet實(shí)現(xiàn)文件上傳下載功能的第二部分,文件列表展示,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01在jsp頁(yè)面中實(shí)現(xiàn)跳轉(zhuǎn)的方式分享
這篇文章介紹在jsp頁(yè)面中實(shí)現(xiàn)跳轉(zhuǎn)的方式,有需要的朋友可以參考一下2013-10-10JSP動(dòng)態(tài)實(shí)現(xiàn)web網(wǎng)頁(yè)登陸和注冊(cè)功能
這篇文章主要介紹是動(dòng)態(tài)實(shí)現(xiàn)web網(wǎng)頁(yè)登陸和注冊(cè)功能的示例代碼,文中代碼講解詳細(xì),對(duì)我們學(xué)習(xí)JSP有一定的幫助,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12