怎么实现分页功能?


目录:

关于分页?

实现数据分页?

分页优化:

模糊查询的优化:

数据库编写SQL语句?

        具体代码展示?


关于分页:

在实现分页功能之前,咱们可以先将主页(index.jsp)表单中的method=“post” 改为method=“get”,方便之后代码的操作,同时还要注释掉破碎重组的相关代码

优势:

* 能显示多条信息

* 减少一定的麻烦

分页操作:

page:当前页数 1

rows:当前显示条数 5

举例:

--【page:1,row:5】1-5

--【page:2,row:5】6-10

--【page:3,row:5】11-15

--【page:4,row:5】....

所以规律应该为:

开始位置 begin:1+(页数-1)*条数

结束位置 end:页数*条数

分页的具体做法:

1. 确定每页显示数据的数量

2. 计算页数

3. 编写SQL语句


实现数据分页:

举例:对于新闻项目,在首页实现数据的分页,让数据显示和进行模糊查询

1.数据库:首先在数据库需要插入多条数据,方便后期分页时查看数据

在数据库里,找到自己的新闻表进行编辑,增加数据:

数据库编写SQL语句:

--错误语句(根据id做判断) 
select * from t_news02 where news_id between 1 and 5

select * from t_news02 where news_id between 6 and 10

用id做判断,新闻的id也会跟着被删除,因此,咱们得用上 伪列 ROWNUM  来编写sql语句:

--列的顺序  伪列:rownum
select a.*,ROWNUM from t_news02 a where ROWNUM between 1 and 5;

图片所示:

注意点:

ROWNUM:每次运行都会从1开始,但是要注意,ROWNUM 不能用于大于1的条件中,大于1不生效,但是如果硬要用大于1的数据做条件,那么我们可以将伪列变成实列


具体代码展示:

2.Eclipse:

NewsDao.java:

package dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;import com.zking.util.DBHelper;import pojo.News;public class NewsDao {private Connection con;private PreparedStatement ps;private ResultSet rs;public List<News> queryByName(String newName,int page/*页数*/) {//int page=1;//页数int rows=5;//条数int begin=1+((page-1)*rows);//开始位置int end=page*rows;//结束位置List<News> list=new ArrayList<News>();try {con=DBHelper.getCon();ps=con.prepareStatement("select * from ("+ "select a.*,ROWNUM myr from t_news02 a where news_title like ?"+ ")b where myr between ? and ? ");ps.setString(1, "%" + newName + "%");ps.setInt(2, begin);ps.setInt(3, end);rs=ps.executeQuery();while(rs.next()) {News news=new News();//给新闻对象赋值news.setNewsId(rs.getInt(1));news.setNewsTitle(rs.getString(2));news.setNewsTopic(rs.getInt(3));news.setNewsAuthor(rs.getString(4));news.setNewsPublisher(rs.getString(5));news.setNewsContent(rs.getString(6));news.setNewsCount(rs.getInt(7));news.setNewsMarker(rs.getInt(8));//将新闻对象添加到集合中list.add(news);}} catch (Exception e) {e.printStackTrace();}finally {DBHelper.close(con, ps, rs);}return list;}//怎么知道当前有几页//数据库数量 / 条数
public int queryCount(String newName) {//int page=1;//页数int rows=5;//条数int count=0;//数据库中数据的数量try {con=DBHelper.getCon();ps=con.prepareStatement("select count(1) from t_news02 where news_title like ?");ps.setString(1, "%" + newName + "%");rs=ps.executeQuery();if(rs.next()) {count=rs.getInt(1);}//返回页数 13.0/5 =2.x->3.0// return Integer.parseInt(Math.ceil(count*1.0/rows)+"");return new Double(Math.ceil(count*1.0/rows)).intValue();} catch (Exception e) {e.printStackTrace();}finally {DBHelper.close(con, ps, rs);}return 0;
}}

index.jsp:

<%@page import="dao.NewsDao"%>
<%@page import="pojo.News"%>
<%@page import="java.nio.charset.StandardCharsets"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="zh"><head><meta charset="UTF-8"><title>bootstrap</title><meta content="width=device-width, initial-scale=1" name="viewport"><link href="/web04/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet"><script src="/web04/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script><script src="/web04/bootstrap-3.3.7-dist/js/bootstrap.js"></script><style>* {outline: none !important;}body,html {background: #7f8d90;}nav,.breadcrumb {border-radius: 0px !important;margin-bottom: 0px !important;}.breadcrumb {margin-bottom: 20px !important;background: #36485c;color: white;}li h4 {width: 300px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;}.breadcrumb .active {color: yellow;}</style>
</head><body>
<%--inclide 包含 --%>
<%@include file="top.jsp" %><ol class="breadcrumb"><li>您当前的位置是</li><li>新闻发布系统</li><li class="active">首页</li>
</ol><%
//点击了表单之后 跳转的是当前这个页面 同时携带一个newName过来(查询的关键字)String newName=request.getParameter("newName");if(newName==null){newName="";//查询全部}//破碎重组【增高】(字符串变成字节数组,再把字节数组重新变成字符串)手动指定编码
//  new String(newName.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);
//  newName=new String(newName.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);%><form action="${pageContext.request.contextPath}/news/index.jsp" class="form-inline" style="margin: 0px auto 20px;" method="get"><div class="form-group" style="display: block;text-align: center;"><div class="input-group"><div class="input-group-addon">新闻标题</div><input name="newName" value="newName" class="form-control" placeholder="请在此输入搜索的关键字" type="text"><span class="input-group-btn"><button class="btn btn-primary" type="submit">搜索												

JavaWeb.09.新闻之分页功能相关推荐

  1. Web09——新闻数据分页

    hellohello!家人们我来啦~ 今天主要学习分页功能哦 目录 一.分页的好处 二.分页显示的步骤 三.实现新闻的分页功能 1.数据库中的操作 2.Eclipse中的操作 一.分页的好处 可以更方 ...

  2. 复习JavaWeb的小项目书籍信息的增删改查分页功能实现Java面试题Session和Cookie的基础概念生活【记录一个咸鱼大学生三个月的奋进生活】034

    记录一个咸鱼大学生三个月的奋进生活034 JavaWeb的增删改查分页功能实现 前期准备工作(数据库连接类和实体类) 数据库建立 数据库连接类(DBManager) 书籍信息的实体类(Book) 操作 ...

  3. jsp自定义图文新闻列表标签结合ssh2,带分页功能

    jsp自定义图文新闻列表标签结合ssh2,带分页功能(欢迎大家讨论指点,共同进步) 1.service层 (模拟返回数据) package com.mingda.service.impl;import ...

  4. javaweb新闻数据分页

    目录 序言: 1.分页显示的步骤 确定每页显示数据的数量 计算显示的页数 编写SQL语句 2.使用分页功能的案例 数据库编译语句: 新闻发布系统改动: 主页(index): NewsDao 序言: 在 ...

  5. javaWeb网页分页功能

    分页主要功能 采用分页技术让数据分批显示,当一页数据太多用户观看体验不佳,就可以使用分页技术 实现分页三部曲 一.确定每一页数据的条数 二.计算页数的数量 三.实现分页的sql语句 3.案例:使用分页 ...

  6. javaWEB——新闻系统部分功能

    目录 后端主页面的模糊查询功能 前端主界面 前端新闻内容界面 新增评论处理界面 删除评论界面 后端主页面的模糊查询功能 原来是关于主页面的所有显示的基础上 1.添加模糊查询的相关标签 2.设置form ...

  7. 《JavaWeb从入门到改行》分页功能的实现

    @目录 什么是分页 ? 两个子模块功能的问题分析 和 解决方案 有条件查和无条件查询的影响 和 解决方案 项目案例: mysql + commons-dbutils+itcast-tools+Base ...

  8. EF/SQL/新闻中分页应用

    1.EF分页 public IList<V_Test> GetTestPageLoad(int pagesize, int pageindex, out int total){try{Te ...

  9. python操作三大主流数据库(6)python操作mysql⑥新闻管理后台功能的完善(增、ajax异步删除新闻、改、查)...

    python操作mysql⑥新闻管理后台功能的完善(增.删.改.查) 安装表单验证 D:\python\python_mysql_redis_mongodb\version02>pip inst ...

最新文章

  1. 客户端修改文件服务器密码,客户端服务器登录 密码问题
  2. DM***+EZ***
  3. 用 Blink 打造你的技术朋友圈
  4. android 代码生成表格,AndroidExcel
  5. 【MySQL】深入浅出剖析mysql事务锁机制 - 笔记
  6. mfc如何删除lineto画的_有哪些好用的板绘软件?衣服上的花纹怎么画?
  7. 数据探索性分析_探索性数据分析
  8. Kubernetes 日志查询分析实践
  9. 数据封装以及解封的过程
  10. Apache Flink 漫谈系列(06) - 流表对偶(duality)性
  11. 第一次作业 四班05
  12. mysql explode函数_hive中,lateral view 与 explode函数
  13. mysql数据类型强转
  14. 如何在excel中单独冻结多行或多列
  15. Day10安卓 专高 day10 ContentProvider内容提供者
  16. 【路由器】OpenWrt 简介和安装
  17. 【工具篇】Joystick Pack摇杆使用Unity多场景使用摇杆
  18. 在元宇宙的概念之下,互联网与数字经济不再是水火不容的存在
  19. mysql语句统计总数_一条sql语句实现统计查询_MySQL
  20. 给中年工程师的忠告[转载]

热门文章

  1. 光缆单盘检测与光缆线路测试需使用双窗口吗?
  2. java定义字符串数组_java字符数组用法总结,java字符串数组的定义与使用
  3. VM虚拟机下载与安装
  4. 1024程序员节|基于Springboot实现运动场馆预约信息管理系统
  5. 图解AI数学基础(2) | 概率与统计(要点速查清单·完结)
  6. 【传智播客】Javaweb程序设计任务教程 黑马程序员 第7,8,9,10,11,12,13,14,15章 课后答案
  7. 跟平庸和解?可笑,世界未曾跟穷人和解。
  8. 电脑win10显示依赖服务器,Win10系统弹出错误1068依赖服务或组无法启动如何解决...
  9. 微计算机原理与接口,微计算机原理与接口技术概要
  10. 51单片机—— PWM、呼吸灯