分页实现(二) java实现

本文介绍了一个基于Java的新闻管理系统中分页功能的具体实现方式,包括News实体类定义、DAO层接口及其实现、Service层接口及实现,以及前端展示的Servlet和JSP页面设计。该系统使用MySQL作为数据库,并通过连接池进行数据库操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

package cn.csdn.web.domain;

import java.io.Serializable;

public class News implements Serializable{
 /**
  *
  */
 private static final long serialVersionUID = 1L;
 private Integer id;
 private String title;
 private String content;
 public News(Integer id, String title, String content) {
  super();
  this.id = id;
  this.title = title;
  this.content = content;
 }
 public News() {
  super();
  // TODO Auto-generated constructor stub
 }
 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
 public static long getSerialversionuid() {
  return serialVersionUID;
 }
 @Override
 public String toString() {
  return "News [id=" + id + ", title=" + title + ", content=" + content
    + "]";
 }
 
}

package cn.csdn.web.dao;

import java.util.List;

import cn.csdn.web.domain.News;

public interface NewsDao {
 Integer getCountRecord();
 List<News> findNowPageInfo(Integer nowpage);
 List<News>findAll();
 Integer getCountPage();
 List<News> findNowPageInfo2(Integer nowpage);
}

package cn.csdn.web.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import cn.csdn.web.domain.News;

public class NewsDaoImpl implements NewsDao {

 private static final Integer PAGESIZE = 10;
 private Integer countPage;
 private Integer countrecorde;
 private static final String URL = "jdbc:mysql://localhost:3306/user?user=root&password=root&useUnicode=true&characterEncoding=UTF8";

 private static Connection conn;
 private PreparedStatement pstmt;
 private ResultSet rs;
 private CallableStatement st;

 static {
  try {
   Class.forName("com.mysql.jdbc.Driver");
   try {
    conn = DriverManager.getConnection(URL);
    System.out.println(conn);
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

 public Integer getCountRecord() {
  String sql = "select count(*) from body";
  try {
   pstmt = conn.prepareStatement(sql);
   rs = pstmt.executeQuery();
   if (rs.next()) {
    countrecorde = rs.getInt(1);
    //this.countPage = this.getCountPag%this.PAGESIZE=0?
   }

  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  return countrecorde;
 }

 public List<News> findNowPageInfo(Integer nowpage) {
  List<News> entities = new ArrayList<News>();
  String sql = "select id,title,content from body limit ?,?";
  try {
   pstmt = conn.prepareStatement(sql);
   int index = 1;
   pstmt.setObject(index++, (nowpage - 1) * PAGESIZE);
   pstmt.setObject(index++, this.PAGESIZE);

 rs = pstmt.executeQuery();
   while (rs.next()) {
    News entity = new News();
    entity.setId(rs.getInt("id"));
    entity.setTitle(rs.getString("title"));
    entity.setContent(rs.getString("content"));
    entities.add(entity);
   }
   release(rs,pstmt);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return entities;
 }
 public Integer getCountPage() {
  this.countPage = this.countrecorde%this.PAGESIZE==0?this.countrecorde/this.PAGESIZE:this.countrecorde/this.PAGESIZE+1;
   return this.countPage;
 }
public List<News> findAll() {
  List<News> entities = new ArrayList<News>();
  String sql = "select id,title,content from body";
  try {
   pstmt = conn.prepareStatement(sql);

   rs = pstmt.executeQuery();
   while (rs.next()) {
    News entity = new News();
    entity.setId(rs.getInt("id"));
    entity.setTitle(rs.getString("title"));
    entity.setContent(rs.getString("content"));
   }
   release(rs,pstmt);
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return entities;
 }
 private void release(ResultSet rs, PreparedStatement pstmt) {
  if(rs!=null){
   try {
    rs.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  if(pstmt!=null){
   try {
    pstmt.close();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

package cn.csdn.web.service;

import cn.csdn.web.dao.NewsDao;

public interface NewsService extends NewsDao{

}

package cn.csdn.web.service;

import java.util.List;

import cn.csdn.web.dao.NewsDao;
import cn.csdn.web.dao.NewsDaoImpl;
import cn.csdn.web.domain.News;

public class NewsServiceImpl implements NewsService{
 
 private NewsDao uDao = new NewsDaoImpl();

 public Integer getCountRecord() {
  // TODO Auto-generated method stub
  return uDao.getCountRecord();
 }

 public List<News> findNowPageInfo(Integer nowpage) {
  // TODO Auto-generated method stub
  return uDao.findNowPageInfo(nowpage);
 }

 public List<News> findAll() {
  // TODO Auto-generated method stub
  return uDao.findAll();
 }

package cn.csdn.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 

import cn.csdn.web.domain.News;
import cn.csdn.web.service.NewsService;
import cn.csdn.web.service.NewsServiceImpl;

public class NewsServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;

 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
   throws ServletException, IOException {
  NewsService newsService = new NewsServiceImpl();
  Integer countRecord = newsService.getCountRecord();
  Integer countPage = newsService.getCountPage();
        String nopage = req.getParameter("nopage");
        int nowpage= 1;
  if(nopage==null||nopage.equals("")){
     nowpage = 1;   
    }else{
     nowpage = Integer.parseInt(nopage);  
    }
  if(nowpage>=countPage){
          nowpage = countPage;
    }
  if(nowpage<=1){
   nowpage = 1; 
  }
  List<News> entities = newsService.findNowPageInfo(nowpage);
  req.setAttribute("countRecord",countRecord);
  req.setAttribute("countPage",countPage);
  req.setAttribute("nowpage", nowpage);
  req.setAttribute("entities",entities);
  req.getRequestDispatcher("./index.jsp").forward(req, resp);

 }

 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  this.doGet(request, response);

 }
}

 public Integer getCountPage() {
  // TODO Auto-generated method stub
  return uDao.getCountPage();
 }

 public List<News> findNowPageInfo2(Integer nowpage) {
  // TODO Auto-generated method stub
  return uDao.findNowPageInfo2(nowpage);
 }

}

 

jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="
http://java.sun.com/jsp/jstl/core" prefix="c"  %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
   <div>
  
   <a href="${pageContext.request.contextPath}/news.do">查看所有新闻信息</a>
     <div>
       <table border="1px" cellpadding="0" cellspacing="0">
        <thead>
        <tr>
        
        <th>序号</th>
        <th>标题</th>
        <th>内容</th>
        <th>操作</th>
        </tr>
        </thead>
        
        <tbody>
         <c:forEach var="entity" items="${entities}">
         <tr>
          <td>${entity.id}</td>
          <td>${entity.title}</td>
          <td>${entity.content}</td>
          <td>
        <a href="#">增加</a>
        <a href="#">修改</a>
       </td>
         
         </tr>
         </c:forEach>
        </tbody> 
       </table>
       </div>
     <div>
     <span><a href="${pageContext.request.contextPath}/news.do?nopage=1">首页</a></span>
     <span><a href="${pageContext.request.contextPath}/news.do?nopage=${nowpage-1}">上一页</a></span>
     <span><a href="${pageContext.request.contextPath}/news.do?nopage=${nowpage+1}">下一页</a></span>
     <span><a href="${pageContext.request.contextPath}/news.do?nopage=${countPage}">末页</a></span>
     <span>总记录数:${countRecord},总页数:${countPage},当前页面:${nowpage }</span>
     
     </div>
   </div>
  </body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值