针对分页,首先开发一个 PageBean 用来控制页面参数:

Java代码  
  1. package com.longweir;
  2. //分页后的javaBean
  3. import java.sql.*;
  4. import com.longweir.util.*;
  5. public class PageBean {
  6. private int pageSize=5;  // 每页显示的记录数5个
  7. private int currentPage=1;    // 当前页码
  8. private int pageCount=1;      // 总页数
  9. private int totalCount=0;     // 总记录数
  10. // 计算总页数
  11. public void setPageCount()
  12. {
  13. this.pageCount=(this.totalCount-1)/this.pageSize+1;
  14. }
  15. //获取总页数
  16. public int getPagecount()
  17. {
  18. return this.pageCount;
  19. }
  20. //设置并修正当前页码,
  21. public void setCurrentPage(int currentpage) {
  22. //校验当前页码
  23. if (currentPage>this.pageCount)
  24. this.currentPage=this.pageCount;
  25. else if (currentPage<1)
  26. this.currentPage=1;
  27. else
  28. this.currentPage=currentpage;
  29. }
  30. //获取当前页码
  31. public int getCurrentPage() {
  32. return this.currentPage;
  33. }
  34. //获取全部记录数
  35. public int getTotalCount()
  36. {
  37. return this.totalCount;
  38. }
  39. //设置总共记录数
  40. public void setTotalCount(int totalcount)
  41. {
  42. this.totalCount =totalcount;
  43. //设置总共记录数后,同时需要校正计算总页数
  44. this.pageCount=(this.totalCount-1)/this.pageSize+1;
  45. }
  46. public int getPageSize() {
  47. return pageSize;
  48. }
  49. public void setPageSize(int pageSize) {
  50. this.pageSize = pageSize;
  51. //设置每页显示的记录个数后 同时需要校正计算后的总页数
  52. this.pageCount=(this.totalCount-1)/this.pageSize+1;
  53. }
  54. }

okay,然后我们开发一个接口 SpiltPage,接口中共有两个方法。

Java代码  
  1. package com.longweir;
  2. //任何业务逻辑类只要实现了该接口 就可以进行数据的分页显示
  3. import java.util.*;
  4. import com.longweir.*;
  5. public interface SpiltPage {
  6. //根据分页对象中的参数 来分页获取数据
  7. public Collection getPageData(PageBean pagebean) throws Exception;
  8. //获取所有的记录个数
  9. public int getAvailableCount() throws Exception;
  10. }

这样以来,主要的关于分页的方法就完成了,我们开发一个针对数据库表操作的业务逻辑类 ProductUtil,来实现上述接口

下面这个类用来操作product表的数据,将分页或所有数据:

Java代码  
  1. package com.longweir;
  2. /*
  3. * 此类包含所有的产品信息的操作业务逻辑
  4. * */
  5. import java.io.*;
  6. import java.sql.*;
  7. import java.util.*;
  8. import com.longweir.SpiltPage;
  9. import com.longweir.bean.ProductInfoVOBean;
  10. import com.longweir.util.DatabaseConnection;
  11. public class ProductUtil implements SpiltPage {
  12. private Connection conn;
  13. //重写无参构造方法来获取数据库连接
  14. public ProductUtil()
  15. {
  16. this.conn=DatabaseConnection.getConnection();  //获取数据库连接对象
  17. }
  18. //实现接口中的方法 来分页获取数据显示
  19. public Collection getPageData(PageBean pagebean) throws Exception
  20. {
  21. Statement stmt=null;
  22. ResultSet rs=null;
  23. Collection ret=new ArrayList();
  24. if (conn.isClosed())  conn=DatabaseConnection.getConnection();
  25. String sqlstr="select * from productInfo order by productid limit "+(pagebean.getCurrentPage()-1)*pagebean.getPageSize()+","+pagebean.getPageSize();
  26. try
  27. {
  28. stmt=conn.createStatement();
  29. rs=stmt.executeQuery(sqlstr);
  30. while (rs.next())
  31. {
  32. ProductInfoVOBean productInfo=new ProductInfoVOBean();
  33. productInfo.setCategoryid(rs.getString("catid"));
  34. productInfo.setProductname(rs.getString("productName"));
  35. productInfo.setProductid(rs.getString("productid"));
  36. productInfo.setPublishment(rs.getString("publishment"));
  37. productInfo.setPrice(rs.getFloat("price"));
  38. productInfo.setDescription(rs.getString("descn"));
  39. ret.add(productInfo);
  40. }
  41. stmt.close();
  42. rs.close();
  43. conn.close();
  44. }
  45. catch (Exception e)
  46. {
  47. e.printStackTrace();
  48. }
  49. return ret;
  50. }
  51. //实现接口方法 获取记录的总共个数
  52. public int getAvailableCount()
  53. {
  54. Statement stmt=null;
  55. ResultSet rs=null;
  56. int counter=0;
  57. try{
  58. if (conn.isClosed()) conn=DatabaseConnection.getConnection();
  59. stmt=conn.createStatement();
  60. rs=stmt.executeQuery("select count(*) from productInfo");
  61. while (rs.next())
  62. {
  63. counter=rs.getInt(1);
  64. }
  65. stmt.close();
  66. rs.close();
  67. conn.close();
  68. }
  69. catch (Exception e){}
  70. return counter;
  71. }

分页的关键技术就是mysql中的这条分页查询语句:

"select * from productInfo order by productid limit "+(pagebean.getCurrentPage()-1)*pagebean.getPageSize()+","+pagebean.getPageSize();

开发一个viewProduct.jsp的页面,来分页显示数据:

Html代码  
  1. <%@ page contentType="text/html;charset=GBK"%>
  2. <%@ page import="java.util.*" %>
  3. <%@ page import="java.io.*" %>
  4. <%@ page import="com.longweir.bean.*" %>
  5. <jsp:useBean id="product" class="com.longweir.ProductUtil" scope="session" />
  6. <jsp:useBean id="pagebean" class="com.longweir.PageBean" scope="session" />
  7. <html>
  8. <head>
  9. <title>查看所有的产品的信息</title>
  10. <link rel="stylesheet" type="text/css" href="css/style.css">
  11. </head>
  12. <body>
  13. <h3 align="center">查看所有的产品信息</h3>
  14. <table width="960" align="center" border="0" cellpadding="2" cellspacing="1" bgcolor="#999999">
  15. <tr bgcolor="#EFEEED">
  16. <td width="100">商品编号</td>
  17. <td width="100">类别</td>
  18. <td width="200">名称</td>
  19. <td width="100">出版商</td>
  20. <td width="80">售价</td>
  21. <td width="200">描述</td>
  22. <td colspan="2" width="100" align="center">管理</td>
  23. </tr>
  24. <%
  25. String temppage=request.getParameter("page");
  26. int pno=1;
  27. if (temppage!=null && !("").equals(temppage))
  28. {
  29. try
  30. {
  31. pno=Integer.parseInt(temppage);  //获取提交的页面编号
  32. }
  33. catch (Exception e)
  34. {
  35. pno=1;   //有异常 则直接跳转到首条
  36. }
  37. }
  38. //每次刷新页面时都应当重新获得表中的记录数,因为翻页过程中表的记录可能随时都会更新
  39. pagebean.setTotalCount(product.getAvailableCount());
  40. pagebean.setCurrentPage(pno);
  41. %>
  42. <%
  43. Collection productproducts=product.getPageData(pagebean);  //分页显示
  44. Iterator it=products.iterator();
  45. while (it.hasNext())
  46. {
  47. ProductInfoVOBean temp=(ProductInfoVOBean)it.next();
  48. out.println("<tr  bgcolor=\"#FFFFFF\">");
  49. out.println("<td>"+temp.getProductid()+"</td>");
  50. out.println("<td>"+temp.getCategoryid()+"</td>");
  51. out.println("<td>"+temp.getProductname()+"</td>");
  52. out.println("<td>"+temp.getPublishment()+"</td>");
  53. out.println("<td>"+temp.getPrice()+"</td>");
  54. out.println("<td>"+temp.getDescription()+"</td>");
  55. out.println("<td algin=\"center\">"+"<a href=#>修改</a>"+"</td>");
  56. out.println("<td align=\"center\">"+"<a href=\"/product/servlet/DeleteProductServlet?productid="+temp.getProductid()+"\">删除</a</td>");
  57. out.println("</tr>");
  58. }
  59. %>
  60. </table>
  61. <table width="960" align="center" border="0" cellpadding="1" cellspacing="2">
  62. <tr>
  63. <td></td>
  64. <tr>
  65. <tr>
  66. <td align="right">
  67. 共<%=pagebean.getPagecount()%>页
  68. <%
  69. for (int i=1;i<=pagebean.getPagecount();i++)
  70. out.println("<a href=/product/viewProduct.jsp?page="+i+">"+i+"</a>");
  71. %>
  72. </td>
  73. </tr>
  74. </table>
  75. </body>
  76. </html>

jsp中有很多java代码,可以考虑使用标签代替现实 呵呵。

分页的效果如下啦:

在jsp中对mysql数据库分页的方法相关推荐

  1. php如何查询数据库,如何在php中查询mysql数据库数据

    如何在php中查询mysql数据库数据 发布时间:2020-07-21 09:23:55 来源:亿速云 阅读:81 作者:Leah 本篇文章给大家分享的是有关如何在php中查询mysql数据库数据,小 ...

  2. Mysql数据库常用查询方法及演示(where条件查询、分页、排序等)

    Mysql数据库常用查询方法及演示 where条件查询的介绍 使用where条件查询可以对表中的数据进行筛选,条件成立的记录会出现在结果集中. where语句支持的运算符: 比较运算符 逻辑运算符 模 ...

  3. cmd查看mysql数据库表_cmd中查看MySQL数据库表数据及结构

    0. 1 .cmd进入mysql安装的bin目录(C:\Program Files\XXXXXX\MySQL Server 5.6\bin) mysql -hlocalhost -uroot -p 回 ...

  4. echarts导入mysql数据库_Echarts最新:Django中从mysql数据库中获取数据传到echarts方式_爱安网 LoveAn.com...

    关于"Echarts"的最新内容 聚合阅读 这篇文章主要介绍了基于vue+echarts 数据可视化大屏展示的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随 ...

  5. FineUIMvc随笔(6)对比WebForms和MVC中表格的数据库分页

    声明:FineUIMvc(基础版)是免费软件,本系列文章适用于基础版. 通过对比WebForms和MVC中表格数据库分页代码的不同,可以对 MVC 中的数据流转有更加深入的了解. WebForms 中 ...

  6. linux mysql cpu 高,Linux系统中关于Mysql数据库导致CPU很高的问题解决

    Linux系统中关于Mysql数据库导致CPU很高的问题解决 发布时间:2007-11-19 00:01:12来源:红联作者:spworks 服务器环境 Liunx AS4 + PHP5 + Mysq ...

  7. Linux中的MySql数据库远程连接

    Linux中的MySql数据库远程连接 rpm–qa |grep mysql安装了以下的包: mysql-libs-5.1.71-1.el6.x86_64 mysql-connector-java-5 ...

  8. python echarts mysql python_Django中从mysql数据库中获取数据传到echarts方式

    尝试了几种方法,感觉过于复杂,于是自己写了一个方法. (1)首先在要绘图的页面传入从数据库中提取的参数,这一步通过views可以实现: (2)然后是页面加载完成时执行的函数ready,调用方法f; ( ...

  9. 在Asp.net core 项目中操作Mysql数据库

    工程环境 : win10+asp.net core 2.1 + vs2017 步骤: 1 在vs中新建asp.net core  项目 2 在Nuget中为项目添加第三方包microsoft.visu ...

最新文章

  1. 用matplotlib显示一下MNIST数据集中手写数字的真实面目
  2. 【Python】递归绘制科赫曲线及科赫雪花及转换成可执行文件打包
  3. Shell 编程基础1 2019-7-6
  4. 使用VHDL编程的直接扩频发生器
  5. [原创].如何解决Nios II SBTE中出现的undefined reference to `xxx'警告
  6. 基本数据类型的自动装箱
  7. 【转】Dicom 学习笔记-Dicom 消息服务(DIMSE-C/DIMSE-N)
  8. wpf 控件生成图片_EyeshotCAD控件生成以及作用
  9. 揭秘人工智能(系列):人工智能带来的网络安全威胁
  10. Python连接MySQL数据库之pymysql模块使用
  11. 3.sf2 核心目录及文件结构
  12. SimpleDateFormat类format方法和parse方法的使用
  13. Head First Python
  14. 6s的充电电流怎么测试软件,用数字万用表测量手机充电器的充电电流的方法和问题...
  15. window远程桌面无法复制粘贴文件到本地
  16. mysql拼音码自动生成_根据中文名,自动生成首字母的拼音码或拼音码(两种方法)...
  17. 单模光纤与多模光纤的区别
  18. 【windows——10 笔记本 好用 快捷键 总结】
  19. 【剑桥摄影协会】针对摄影的显示器校准
  20. 使用服务网格提升应用和网络安全

热门文章

  1. 结构体怎么赋值_c语言学习之基础知识点介绍:结构体的介绍
  2. kotlin int最大值_Kotlin程序查找三个数字中的最大值
  3. openssl java aes_请问如何使用AES对使用OpenSSL命令加密的Java文件进行解密?
  4. 校园计算机网络系统,校园计算机网络系统
  5. python相关函数_python列表相关函数
  6. 量词逻辑量词里面的v表示?_知识表示能力问答中的人工智能量词(MCQ)
  7. 一个类可以有一个接口,接口可以有一个Java类吗?
  8. 二、华为鸿蒙开发DevEco Studio运行第一个Hello World工程
  9. ubuntu20.10创建QT应用程序快捷方式 Terminal中输入命令直接打开QtCreator
  10. JENKINS使用DOCKER运行PYTEST并且出ALLURE报告