如何用jsp+mysql实现网页的分页查询


一、实现分页查询的核心sql语句

(1)查询数据库的记录总数的sql语句:

select count(*) from +(表名);

(2)每次查询的记录数的sql语句:

其中:0是搜索的索引,2是每次查找的条数。

select * from 表名 limit 0,2;

二、代码实现

在上一篇中,我已经介绍了DBconnection类和Author对象类,前者用于获取数据库连接,后者用于表示作者对象。这两个类的代码点击连接查看。点击链接以查看 DBconnection 类和 Author 对象类

(1)登录页面:index.jsp。

<%@ page language="j*a" 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">用户列表分页查询</a>
</body>
</html>

(2)显示页面:userlistpage.jsp。

<%@ page language="j*a" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://j*a.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>查询页面</title>
</head>
<body>
<table border="1">
  <tr>
    <td>编号</td>
    <td>名称</td>
    <td>价格</td>
    <td>数量</td>
    <td>日期</td>
    <td>风格</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>首页</span>
      <span>上一页</span>
      <a href ="AuthorListPageServlet?currPage=${pageBean.currentPage + 1 }">下一页</a>
      <a href ="AuthorListPageServlet?currPage=${pageBean.totalPage }">尾页</a>
      </c:if>
      
      <c:if test="${pageBean.currentPage > 1 && pageBean.currentPage < pageBean.totalPage  }">
       <a href ="AuthorListPageServlet?currPage=1">首页</a>
      <a href ="AuthorListPageServlet?currPage=${pageBean.currentPage - 1 }">上一页</a>
      <a href ="AuthorListPageServlet?currPage=${pageBean.currentPage + 1 }">下一页</a>
      <a href ="AuthorListPageServlet?currPage=${pageBean.totalPage }">尾页</a>
      </c:if>
     
     <c:if test="${ pageBean.currentPage >= pageBean.totalPage}">
      <a href ="AuthorListPageServlet?currPage=1">首页</a>
      <a href ="AuthorListPageServlet?currPage=${pageBean.currentPage - 1 }">上一页</a>
     <span>下一页</span>
     <span>尾页</span>
     </c:if>
</div>
</c:if>
</body>
</html>

(3)功能实现:AuthorDao.j*a。

package com.dao;

import j*a.sql.Connection;
import j*a.sql.PreparedStatement;
import j*a.sql.ResultSet;
import j*a.sql.SQLException;
import j*a.util.ArrayList;
import j*a.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();
            }
                
     }
    
}

(4)交互层:AuthorListPageServlet.j*a。

package com.servlet;

import j*a.io.IOException;
import j*a.util.List;

import j*ax.servlet.ServletException;
import j*ax.servlet.annotation.WebServlet;
import j*ax.servlet.http.HttpServlet;
import j*ax.servlet.http.HttpServletRequest;
import j*ax.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);
    }

}

(5)工具类:PageBean.j*a。作用是:获取结果集。

PHPShops多用户商城系统 PHPShops多用户商城系统

随着电子商务模式更加多样化,企业和个人的迫切需求,PHPShops多用户商城系统正可以为其提供专业的电子商务解决方案。社区化电子商务,主要面向行业类和地方门户类站点。 PHPShops多用户商城系统(简称PHPShops)是基于电子商务的一套平台交易系统,它采用目前最流行网站建设工具PHP+MYSQL,实现模版分离技术,通过HTML交互式网页技术来实行客户端与服务器端的交流。无论在

PHPShops多用户商城系统 0 查看详情 PHPShops多用户商城系统
package com.util;

import j*a.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 < 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;
    }
    
}

三、运行结果

(1)首页:

如何用jsp+mysql实现网页的分页查询

(2)中间页:

如何用jsp+mysql实现网页的分页查询

(3)尾页:

如何用jsp+mysql实现网页的分页查询

以上就是如何用jsp+mysql实现网页的分页查询的详细内容,更多请关注其它相关文章!


# jsp  # 企业为啥要网站建设  # 白色关键词排名团队  # 正安网站关键词优化价格  # 银川网站建设公司是什么  # 四川seo助手方案  # 黄冈服装网站推广公司  # 线上修图师网站推广  # 每页  # 上一页  # 下一页  # 首页  # 如何用  # 尾页  # 多用户  # 镜像  # 分页  # 网站建设工具  # MySQL  # 刷评论平台推广qq网站低价  # 桂林短视频关键词优化排名公司  # 虹口区外贸网站建设方案 


相关栏目: 【 Google疑问12 】 【 Facebook疑问10 】 【 优化推广96088 】 【 技术知识133117 】 【 IDC资讯59369 】 【 网络运营7196 】 【 IT资讯61894


相关推荐: 微信网页版在线登录 微信网页版在线使用入口  Lar*el如何创建自定义的辅助函数(Helpers)_Lar*el全局函数定义与加载方法  铁路12306怎么申请退票_铁路12306退票申请操作流程  t3出行如何使用微信支付  b站网页版入口 哔哩哔哩官方网站直接进入  解决 Vue 3 组件未定义错误:理解 createApp 与根组件的正确使用  J*aScript 数值去小数位处理:多种方法与实践  米侠浏览器插件无法启用怎么办 米侠浏览器扩展兼容性修复  火柴人战争网页版在线玩  AO3官方镜像链接 | 最新防走失网址永久收藏  研招网官方网站正版登录网址_中国研究生招生信息网官网首页  怎样让Windows 11的开始菜单恢复经典样式_Open-Shell工具使用指南【怀旧】  使用Python和NLTK从文本中高效提取名词的实用教程  虫虫漫画绿色安全入口_虫虫漫画绿色安全入口安全看漫画  Go语言反射机制:如何访问被嵌入结构体遮蔽的方法  微信步数怎么刷_微信步数快速提升技巧  全球各国上班时间表外贸邮件时间  惠普电脑BIOS界面看不懂怎么办_HP电脑BIOS功能选项解读与设置  PHP动态导航按钮:根据用户登录状态切换链接与文本  《地下城堡4:骑士与破碎编年史》墓穴挑战125攻略  Mac如何开启画中画模式_Mac Safari浏览器视频画中画功能  《爱笔思画x》涂色教程  谷歌浏览器官网地址整理_谷歌浏览器新版直连2026稳定访问  Golang如何测试结构体方法_Golang reflect方法测试与调用技巧  《盗墓笔记手游》技能介绍  Go反射进阶:访问内嵌结构体中的被遮蔽方法  Pandas中基于动态偏移量实现DataFrame列值位移的策略  Dagster资产间数据传递与用户配置管理教程  iPhone 15 Pro如何查看存储空间占用_iPhone 15 Pro存储空间查看教程  小米手机屏幕失灵乱跳怎么办 屏幕触控问题自检与临时解决方法【应急】  Lar*el Eloquent中通过Join查询关联数据表:解决多行子查询问题  Lar*el Dusk 测试中管理浏览器权限:以剪贴板访问为例  J*a实现任务清单管理_集合框架综合入门练手  Yandex无需登录畅游 俄罗斯搜索引擎最新官网指南  mysql怎么导入sql文件_mysql导入sql文件的方法与技巧  手机自动关机是怎么回事?如何修复?手机异常关机的原因排查与修复技巧  Mac hosts文件在哪里_Mac修改hosts文件详细教程  《via浏览器》强制缩放网页设置方法  如何在Golang中处理表单文件上传_Golang 表单文件上传示例  优化Flask模板中SQLAlchemy查询迭代标签:处理字符串空格问题  163邮箱网页版入口 163邮箱在线使用  以下哪一个是适应长期护理制度发展而设立的新职业  《书耽》更换手机号方法  国际经济与贸易就业方向解析  鸣潮历史学家灯塔位置一览  如何编写一个符合 composer 规范的 post-install-cmd 脚本?  顺丰官方查单号入口 顺丰快递单号查询官网入口  圆通快递官网入口查询单号 手机版官方查询入口  HTML Canvas文本样式定制指南:解决外部字体加载与应用难题  sublime怎么在文件中显示代码结构大纲_sublime符号列表功能 

 2023-05-30

了解您产品搜索量及市场趋势,制定营销计划

同行竞争及网站分析保障您的广告效果

点击免费数据支持

提交您的需求,1小时内享受我们的专业解答。

运城市盐湖区信雨科技有限公司


运城市盐湖区信雨科技有限公司

运城市盐湖区信雨科技有限公司是一家深耕海外推广领域十年的专业服务商,作为谷歌推广与Facebook广告全球合作伙伴,聚焦外贸企业出海痛点,以数字化营销为核心,提供一站式海外营销解决方案。公司凭借十年行业沉淀与平台官方资源加持,打破传统外贸获客壁垒,助力企业高效开拓全球市场,成为中小企业出海的可靠合作伙伴。

 8156699

 13765294890

 8156699@qq.com

Notice

We and selected third parties use cookies or similar technologies for technical purposes and, with your consent, for other purposes as specified in the cookie policy.
You can consent to the use of such technologies by closing this notice, by interacting with any link or button outside of this notice or by continuing to browse otherwise.