Web09——新闻数据分页

hellohello!家人们我来啦~ 今天主要学习分页功能哦

目录

一.分页的好处

二.分页显示的步骤

三.实现新闻的分页功能

1.数据库中的操作

2.Eclipse中的操作


一.分页的好处

可以更方便的浏览页面,不用因为数据太多而导致的麻烦滑动

二.分页显示的步骤

三.实现新闻的分页功能

1.数据库中的操作

<1>.建表

<2>.加数据

<3>.找规律

<4>.编写sql语句

 

 


2.Eclipse中的操作

   dao方法:

package com.zking.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.zking.pojo.News;
import com.zking.util.DBHelper;

public class NewsDao {

	private Connection con;
	private PreparedStatement ps;
	private ResultSet rs;

	public List<News> queryByName(String newName,int page/*页数*/) {
		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_news 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.setNewId(rs.getInt(1));
				news.setNewTitle(rs.getString(2));
				news.setNewTopic(rs.getInt(3));
				news.setNewAuthor(rs.getString(4));
				news.setNewPublisher(rs.getString(5));
				news.setNewsContent(rs.getString(6));
				news.setNewsClink(rs.getInt(7));
				news.setNewsLove(rs.getInt(8));

				// 将新闻对象添加到集合中
				list.add(news);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.getClose(con, ps, rs);
		}
		return list;
	}
	
	
	//怎么知道当前有几页
	public int queryCount(String newName) {
		int rows=5;//条数
		int count=0;//数据库中数据的数量 
		try {
			con = DBHelper.getCon();
			ps = con.prepareStatement("select count(1) from t_news where news_title like ?");
			ps.setString(1, "%" + newName + "%");
			rs = ps.executeQuery();
			if(rs.next()) {
			  count=rs.getInt(1);//数据库的条数 
			}
			//返回页数
			//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.getClose(con, ps, rs);
		}
		return 0;
	}

}

       

      index首页


<%@page import="com.zking.dao.NewsDao"%>
<%@page import="com.zking.pojo.News"%>
<%@page import="java.nio.charset.StandardCharsets"%>
<%@page import="oracle.jdbc.driver.OracleDriver"%>
<%@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 name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath}/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>
<%request.setCharacterEncoding("utf-8"); //防乱码 %>
<body>

<!-- @include 包含 -->
<%@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="";//变成空字符串,这是完整的模糊查询
}

%>
<form atcion="${pageContext.request.contextPath}/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 %>" type="text" class="form-control" placeholder="请在此输入搜索的关键字">
            <span class="input-group-btn">
                    <button type="submit" class="btn btn-primary">搜索🔍</button>
                </span>
        </div>
    </div>
</form>

<div class="container">
<ul class="list-group">
<%
//设定 当前用户进行分页的名字叫做page
String index=request.getParameter("page");
int n=1;//默认页数第一页
if(index!=null){
	n=Integer.parseInt(index);//设置为你携带的页数
}


//破碎重组:(防止乱码的另外一种方法)
//newName=new String(newName.getBytes(StandardCharsets.ISO_8859_1),StandardCharsets.UTF_8);

//查询当前数据的对应页数
int MaxPage=new NewsDao().queryCount(newName);


//根据页数查询数据
for(News news:new NewsDao().queryByName(newName,n)){ 
%>
        <li class="list-group-item">
             <h4 class="list-group-item-heading">
                <a href="${pageContext.request.contextPath}/read.jsp?newId=<%=news.getNewId() %>" data-toggle="tooltip" data-placement="bottom" title="<%=news.getNewTitle()%>">
                   <%=news.getNewTitle()%>
                </a>
             </h4>
            <p class="list-group-item-text text-right">s
                <span class="glyphicon glyphicon-user"><code><%=news.getNewAuthor()%></code></span>
                <span class="glyphicon glyphicon-eye-open"><code><%=news.getNewsClink() %></code></span>
                <span class="glyphicon glyphicon-tag"><code><%=news.getNewsLove() %></code></span>
                <span class="glyphicon glyphicon-time"><code><%=news.getNewPublisher() %></code></span>
            </p>
        </li>
        
    </ul>
</div>




<%

}

%>   
<div class="container text-center">
    <ul class="pagination" style="margin: 20px auto;">
       <li>
            <a href="index.jsp?page=<%=Math.max(n-1,1)%>&newName=<%=newName%>"><span>&laquo;</span></a>
        </li>
        <%
           for(int i=1;i<=MaxPage;i++){
        	   
        %>
        <li class="<%=i==n?"active":""%>"><a href="index.jsp?page=<%=i%>&newName=<%=newName%>"><%=i%></a></li>
       <%
           }
       %>
       
        <li>
            <a href="index.jsp?page=<%=Math.min(n+1,1)%>&newName=<%=newName%>"><span>&raquo;</span></a>
        </li>
        
    </ul>
</div>
<script>
    $(function () {
        $('[data-toggle="tooltip"]').tooltip({
            trigger: "hover"
        })
    })
</script>
</body>
</html>


今天的代码到这里就结束啦~下期见!

再忙也要捕捉快乐呀

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值