如何用jsp+mysql实现网页的分页查询
来源:亿速云
时间:2023-04-24 07:03:24 393浏览 收藏
有志者,事竟成!如果你在学习数据库,那么本文《如何用jsp+mysql实现网页的分页查询》,就很适合你!文章讲解的知识点主要包括,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~
一、实现分页查询的核心sql语句
(1)查询数据库的记录总数的sql语句:
select count(*) from +(表名);
(2)每次查询的记录数的sql语句:
其中:0是搜索的索引,2是每次查找的条数。
select * from 表名 limit 0,2;
二、代码实现
*上篇写过这两个类 , DBconnection类:用于获取数据库连接,Author对象类。这两个类的代码点击连接查看。点击链接查看 DBconnection类和Author对象类
(1)登录页面:index.jsp。
nbsp;html> <meta><title>Insert title here</title> <a>用户列表分页查询</a>
(2)显示页面:userlistpage.jsp。
nbsp;html> <meta><title>查询页面</title>
| 编号 | 名称 | 价格 | 数量 | 日期 | 风格 |
| ${author.id} | ${author.name } | ${author.price } | ${author.num } | ${author.dates} | ${author.style} |
(3)功能实现: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();
//获取数据库连接
Connection conn = db.getConn();
String sql="select *from furnitures where name = ? and id = ?";
PreparedStatement ps=conn.prepareStatement(sql);
//设置用户名和密码作为参数放入sql语句
ps.setString(1,username);
ps.setInt(2,password);
//执行查询语句
ResultSet rs = ps.executeQuery();
//用户名和密码正确,查到数据 欧式风格 茶几
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;
}
/**
* 用户列表信息查询
* @return
*/
public List<author> queryAuthorList(){
Author obj = null ;
List<author> list = new ArrayList<author>();
try {
DBConnection db = new DBConnection();
//获取数据库连接
Connection conn = db.getConn();
String sql="select *from furnitures";
PreparedStatement ps=conn.prepareStatement(sql);
//执行查询语句
ResultSet rs = ps.executeQuery();
//用户名和密码正确,查到数据 欧式风格 茶几
//循环遍历获取用户信息
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里边
list.add(obj);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
/**
* 查询用户表总记录数
* @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;
}
/**
* 查询用户分页数据
* @param pageIndex数据起始索引
* @param pageSize每页显示条数
* @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();
//遍历结果集获取用户列表数据
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;
}
/**
* 用户新增
* @param obj
*/
public void add(Author obj) {
try {
DBConnection db = new DBConnection();
//获取数据库连接
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());
//执行sql语句
ps.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//删除用户
public void del(int id) {
try {
DBConnection db = new DBConnection();
//获取数据库连接
Connection conn = db.getConn();
String sql="delete from furnitures where id = ?";
PreparedStatement ps=conn.prepareStatement(sql);
ps.setObject(1, id);
//执行sql语句
ps.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}</author></author></author></author></author></author>(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();
//总记录数
int record = ad.queryUserListCount();
//接收页面传入的页码
String strPage = request.getParameter("currPage");
int currPage = 1;//默认第一页
if(strPage != null) {
currPage = Integer.parseInt(strPage);
}
PageBean<author> pb = new PageBean<author>(currPage,pageSize,record);
//查询某一页的结果集
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);
}
}</author></author></author>(5)工具类:PageBean.java。作用是:获取结果集。
package com.util;
import java.util.List;
public class PageBean<t>{
private int currentPage;//当前页码
private int pageIndex;//数据起始索引
private int pageSize;//每页条数
private int record;//总记录数
private int totalPage;//总页数
private List<t>list;//每页显示的结果集
/**
* 构造方法初始化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;
//总页数
if(record % pageSize == 0) {
//整除,没有多余的页
this.totalPage = record / pageSize;
}
else {
//有多余的数据,在增加一页
this.totalPage = record / pageSize + 1;
}
//计算数据起始索引pageIndex
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;
}
}</t></t></t></t>三、运行结果
(1)首页:

(2)中间页:

(3)尾页:

终于介绍完啦!小伙伴们,这篇关于《如何用jsp+mysql实现网页的分页查询》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布数据库相关知识,快来关注吧!
声明:本文转载于:亿速云 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
113 收藏
-
128 收藏
-
460 收藏
-
349 收藏
-
223 收藏
-
406 收藏
-
486 收藏
-
294 收藏
-
117 收藏
-
411 收藏
-
420 收藏
-
264 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习