在jsp中对mysql数据库分页的方法
针对分页,首先开发一个 PageBean 用来控制页面参数:
- package com.longweir;
- //分页后的javaBean
- import java.sql.*;
- import com.longweir.util.*;
- public class PageBean {
- private int pageSize=5; // 每页显示的记录数5个
- private int currentPage=1; // 当前页码
- private int pageCount=1; // 总页数
- private int totalCount=0; // 总记录数
- // 计算总页数
- public void setPageCount()
- {
- this.pageCount=(this.totalCount-1)/this.pageSize+1;
- }
- //获取总页数
- public int getPagecount()
- {
- return this.pageCount;
- }
- //设置并修正当前页码,
- public void setCurrentPage(int currentpage) {
- //校验当前页码
- if (currentPage>this.pageCount)
- this.currentPage=this.pageCount;
- else if (currentPage<1)
- this.currentPage=1;
- else
- this.currentPage=currentpage;
- }
- //获取当前页码
- public int getCurrentPage() {
- return this.currentPage;
- }
- //获取全部记录数
- public int getTotalCount()
- {
- return this.totalCount;
- }
- //设置总共记录数
- public void setTotalCount(int totalcount)
- {
- this.totalCount =totalcount;
- //设置总共记录数后,同时需要校正计算总页数
- this.pageCount=(this.totalCount-1)/this.pageSize+1;
- }
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- //设置每页显示的记录个数后 同时需要校正计算后的总页数
- this.pageCount=(this.totalCount-1)/this.pageSize+1;
- }
- }
okay,然后我们开发一个接口 SpiltPage,接口中共有两个方法。
- package com.longweir;
- //任何业务逻辑类只要实现了该接口 就可以进行数据的分页显示
- import java.util.*;
- import com.longweir.*;
- public interface SpiltPage {
- //根据分页对象中的参数 来分页获取数据
- public Collection getPageData(PageBean pagebean) throws Exception;
- //获取所有的记录个数
- public int getAvailableCount() throws Exception;
- }
这样以来,主要的关于分页的方法就完成了,我们开发一个针对数据库表操作的业务逻辑类 ProductUtil,来实现上述接口
下面这个类用来操作product表的数据,将分页或所有数据:
- package com.longweir;
- /*
- * 此类包含所有的产品信息的操作业务逻辑
- * */
- import java.io.*;
- import java.sql.*;
- import java.util.*;
- import com.longweir.SpiltPage;
- import com.longweir.bean.ProductInfoVOBean;
- import com.longweir.util.DatabaseConnection;
- public class ProductUtil implements SpiltPage {
- private Connection conn;
- //重写无参构造方法来获取数据库连接
- public ProductUtil()
- {
- this.conn=DatabaseConnection.getConnection(); //获取数据库连接对象
- }
- //实现接口中的方法 来分页获取数据显示
- public Collection getPageData(PageBean pagebean) throws Exception
- {
- Statement stmt=null;
- ResultSet rs=null;
- Collection ret=new ArrayList();
- if (conn.isClosed()) conn=DatabaseConnection.getConnection();
- String sqlstr="select * from productInfo order by productid limit "+(pagebean.getCurrentPage()-1)*pagebean.getPageSize()+","+pagebean.getPageSize();
- try
- {
- stmt=conn.createStatement();
- rs=stmt.executeQuery(sqlstr);
- while (rs.next())
- {
- ProductInfoVOBean productInfo=new ProductInfoVOBean();
- productInfo.setCategoryid(rs.getString("catid"));
- productInfo.setProductname(rs.getString("productName"));
- productInfo.setProductid(rs.getString("productid"));
- productInfo.setPublishment(rs.getString("publishment"));
- productInfo.setPrice(rs.getFloat("price"));
- productInfo.setDescription(rs.getString("descn"));
- ret.add(productInfo);
- }
- stmt.close();
- rs.close();
- conn.close();
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- return ret;
- }
- //实现接口方法 获取记录的总共个数
- public int getAvailableCount()
- {
- Statement stmt=null;
- ResultSet rs=null;
- int counter=0;
- try{
- if (conn.isClosed()) conn=DatabaseConnection.getConnection();
- stmt=conn.createStatement();
- rs=stmt.executeQuery("select count(*) from productInfo");
- while (rs.next())
- {
- counter=rs.getInt(1);
- }
- stmt.close();
- rs.close();
- conn.close();
- }
- catch (Exception e){}
- return counter;
- }
分页的关键技术就是mysql中的这条分页查询语句:
"select * from productInfo order by productid limit "+(pagebean.getCurrentPage()-1)*pagebean.getPageSize()+","+pagebean.getPageSize();
开发一个viewProduct.jsp的页面,来分页显示数据:
- <%@ page contentType="text/html;charset=GBK"%>
- <%@ page import="java.util.*" %>
- <%@ page import="java.io.*" %>
- <%@ page import="com.longweir.bean.*" %>
- <jsp:useBean id="product" class="com.longweir.ProductUtil" scope="session" />
- <jsp:useBean id="pagebean" class="com.longweir.PageBean" scope="session" />
- <html>
- <head>
- <title>查看所有的产品的信息</title>
- <link rel="stylesheet" type="text/css" href="css/style.css">
- </head>
- <body>
- <h3 align="center">查看所有的产品信息</h3>
- <table width="960" align="center" border="0" cellpadding="2" cellspacing="1" bgcolor="#999999">
- <tr bgcolor="#EFEEED">
- <td width="100">商品编号</td>
- <td width="100">类别</td>
- <td width="200">名称</td>
- <td width="100">出版商</td>
- <td width="80">售价</td>
- <td width="200">描述</td>
- <td colspan="2" width="100" align="center">管理</td>
- </tr>
- <%
- String temppage=request.getParameter("page");
- int pno=1;
- if (temppage!=null && !("").equals(temppage))
- {
- try
- {
- pno=Integer.parseInt(temppage); //获取提交的页面编号
- }
- catch (Exception e)
- {
- pno=1; //有异常 则直接跳转到首条
- }
- }
- //每次刷新页面时都应当重新获得表中的记录数,因为翻页过程中表的记录可能随时都会更新
- pagebean.setTotalCount(product.getAvailableCount());
- pagebean.setCurrentPage(pno);
- %>
- <%
- Collection productproducts=product.getPageData(pagebean); //分页显示
- Iterator it=products.iterator();
- while (it.hasNext())
- {
- ProductInfoVOBean temp=(ProductInfoVOBean)it.next();
- out.println("<tr bgcolor=\"#FFFFFF\">");
- out.println("<td>"+temp.getProductid()+"</td>");
- out.println("<td>"+temp.getCategoryid()+"</td>");
- out.println("<td>"+temp.getProductname()+"</td>");
- out.println("<td>"+temp.getPublishment()+"</td>");
- out.println("<td>"+temp.getPrice()+"</td>");
- out.println("<td>"+temp.getDescription()+"</td>");
- out.println("<td algin=\"center\">"+"<a href=#>修改</a>"+"</td>");
- out.println("<td align=\"center\">"+"<a href=\"/product/servlet/DeleteProductServlet?productid="+temp.getProductid()+"\">删除</a</td>");
- out.println("</tr>");
- }
- %>
- </table>
- <table width="960" align="center" border="0" cellpadding="1" cellspacing="2">
- <tr>
- <td></td>
- <tr>
- <tr>
- <td align="right">
- 共<%=pagebean.getPagecount()%>页
- <%
- for (int i=1;i<=pagebean.getPagecount();i++)
- out.println("<a href=/product/viewProduct.jsp?page="+i+">"+i+"</a>");
- %>
- </td>
- </tr>
- </table>
- </body>
- </html>
jsp中有很多java代码,可以考虑使用标签代替现实 呵呵。
分页的效果如下啦:
在jsp中对mysql数据库分页的方法相关推荐
- php如何查询数据库,如何在php中查询mysql数据库数据
如何在php中查询mysql数据库数据 发布时间:2020-07-21 09:23:55 来源:亿速云 阅读:81 作者:Leah 本篇文章给大家分享的是有关如何在php中查询mysql数据库数据,小 ...
- Mysql数据库常用查询方法及演示(where条件查询、分页、排序等)
Mysql数据库常用查询方法及演示 where条件查询的介绍 使用where条件查询可以对表中的数据进行筛选,条件成立的记录会出现在结果集中. where语句支持的运算符: 比较运算符 逻辑运算符 模 ...
- cmd查看mysql数据库表_cmd中查看MySQL数据库表数据及结构
0. 1 .cmd进入mysql安装的bin目录(C:\Program Files\XXXXXX\MySQL Server 5.6\bin) mysql -hlocalhost -uroot -p 回 ...
- echarts导入mysql数据库_Echarts最新:Django中从mysql数据库中获取数据传到echarts方式_爱安网 LoveAn.com...
关于"Echarts"的最新内容 聚合阅读 这篇文章主要介绍了基于vue+echarts 数据可视化大屏展示的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随 ...
- FineUIMvc随笔(6)对比WebForms和MVC中表格的数据库分页
声明:FineUIMvc(基础版)是免费软件,本系列文章适用于基础版. 通过对比WebForms和MVC中表格数据库分页代码的不同,可以对 MVC 中的数据流转有更加深入的了解. WebForms 中 ...
- linux mysql cpu 高,Linux系统中关于Mysql数据库导致CPU很高的问题解决
Linux系统中关于Mysql数据库导致CPU很高的问题解决 发布时间:2007-11-19 00:01:12来源:红联作者:spworks 服务器环境 Liunx AS4 + PHP5 + Mysq ...
- Linux中的MySql数据库远程连接
Linux中的MySql数据库远程连接 rpm–qa |grep mysql安装了以下的包: mysql-libs-5.1.71-1.el6.x86_64 mysql-connector-java-5 ...
- python echarts mysql python_Django中从mysql数据库中获取数据传到echarts方式
尝试了几种方法,感觉过于复杂,于是自己写了一个方法. (1)首先在要绘图的页面传入从数据库中提取的参数,这一步通过views可以实现: (2)然后是页面加载完成时执行的函数ready,调用方法f; ( ...
- 在Asp.net core 项目中操作Mysql数据库
工程环境 : win10+asp.net core 2.1 + vs2017 步骤: 1 在vs中新建asp.net core 项目 2 在Nuget中为项目添加第三方包microsoft.visu ...
最新文章
- 用matplotlib显示一下MNIST数据集中手写数字的真实面目
- 【Python】递归绘制科赫曲线及科赫雪花及转换成可执行文件打包
- Shell 编程基础1 2019-7-6
- 使用VHDL编程的直接扩频发生器
- [原创].如何解决Nios II SBTE中出现的undefined reference to `xxx'警告
- 基本数据类型的自动装箱
- 【转】Dicom 学习笔记-Dicom 消息服务(DIMSE-C/DIMSE-N)
- wpf 控件生成图片_EyeshotCAD控件生成以及作用
- 揭秘人工智能(系列):人工智能带来的网络安全威胁
- Python连接MySQL数据库之pymysql模块使用
- 3.sf2 核心目录及文件结构
- SimpleDateFormat类format方法和parse方法的使用
- Head First Python
- 6s的充电电流怎么测试软件,用数字万用表测量手机充电器的充电电流的方法和问题...
- window远程桌面无法复制粘贴文件到本地
- mysql拼音码自动生成_根据中文名,自动生成首字母的拼音码或拼音码(两种方法)...
- 单模光纤与多模光纤的区别
- 【windows——10 笔记本 好用 快捷键 总结】
- 【剑桥摄影协会】针对摄影的显示器校准
- 使用服务网格提升应用和网络安全
热门文章
- 结构体怎么赋值_c语言学习之基础知识点介绍:结构体的介绍
- kotlin int最大值_Kotlin程序查找三个数字中的最大值
- openssl java aes_请问如何使用AES对使用OpenSSL命令加密的Java文件进行解密?
- 校园计算机网络系统,校园计算机网络系统
- python相关函数_python列表相关函数
- 量词逻辑量词里面的v表示?_知识表示能力问答中的人工智能量词(MCQ)
- 一个类可以有一个接口,接口可以有一个Java类吗?
- 二、华为鸿蒙开发DevEco Studio运行第一个Hello World工程
- ubuntu20.10创建QT应用程序快捷方式 Terminal中输入命令直接打开QtCreator
- JENKINS使用DOCKER运行PYTEST并且出ALLURE报告