• jsp mysql 实现客户端简单分页查询


    jsp mysql 实现客户端简单分页查询

     

    dao.impl

    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    import org.apache.commons.dbutils.handlers.ScalarHandler;
    import sim.dao.StudentDao;
    import sim.entity.Student;
    
    import javax.sql.DataSource;
    import java.sql.SQLException;
    import java.util.List;
    
    public class StudentDaoImpl implements StudentDao {
        private DataSource ds = new ComboPooledDataSource();
        private QueryRunner qr = new QueryRunner(ds);  // 必有的两句
    
        @Override
        public List<Student> selectAll(Integer currentPageNo, Integer pageSize) {
            List<Student>  studentList = null;
            try {
                String sql  ="select *  from  user  limit ?,?";
                Object[] objects={(currentPageNo-1)*pageSize,pageSize};  //页面数量,页面大小
                studentList = qr.query(sql,new BeanListHandler<Student>(Student.class),objects);  //很多地方少写了 r currentPageNo
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return studentList;
        }
    
    
        @Override
        public Integer selectCount() {
            Long count = null;
            try {
                String sql ="select count(1) from  user";   //把 Long 写成 long
                count = (Long) qr.query(sql,new ScalarHandler());  //总页数
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return Integer.parseInt(count+"");
        }
    }
    

     

    StudentServiceImpl

    
    import sim.dao.StudentDao;
    import sim.dao.StudentDaoImpl.StudentDaoImpl;
    import sim.entity.Student;
    import sim.service.StudentService;
    
    import java.util.List;
    
    public class StudentServiceImpl implements StudentService {
        private StudentDao studentDao  = new StudentDaoImpl();  //必有的实例
        @Override
        public List<Student> selectAll(Integer currentPageNo, Integer pageSize) {   //
            return studentDao.selectAll(currentPageNo,pageSize);
        }
    
        @Override
        public Integer selectCount() {
            return studentDao.selectCount();
        }
    }
    

     

    servlet

    
    import sim.Utils.PageUtils;
    import sim.entity.Student;
    import sim.service.StudentService;
    import sim.service.StudentServiceImpl.StudentServiceImpl;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.List;
    
    @WebServlet(name = "studentServlet" ,urlPatterns = "/studentServlet")
    public class StudentServlet extends HttpServlet {
        private StudentService studentService;  //一定有的一句
    
        @Override
        public void init() throws ServletException {
            studentService = new StudentServiceImpl();   //一定有的一句
        }
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String currentPageNoStr = req.getParameter("currentPageNo");  //本页的当前页量
            Integer currentPageNo = null;
            if(currentPageNoStr ==null || currentPageNoStr==""){
                currentPageNo = 1;
            }else{
                currentPageNo =Integer.parseInt(currentPageNoStr);
            }
    
            String pageSizeStr = req.getParameter("pageSize");  //当前页量数
            Integer pageSize=null;
            if(pageSizeStr ==null || pageSizeStr==""){
                pageSize = 2;  //无页量,则为2
            }else{
                pageSize =Integer.parseInt(pageSizeStr);  //有页量,则传递
            }
    
            Integer totalPageCount = studentService.selectCount();
            Integer totalPageSize =  totalPageCount  % pageSize ==0 ? totalPageCount / pageSize  //为什么等于0————因为把 % 打成了 /
                    : totalPageCount / pageSize +1;
            List<Student> studentList = studentService.selectAll(currentPageNo,pageSize);
            PageUtils<Student> pageUtils = new PageUtils<>();  //数据库的名字和实体类不同,导致错误。
            pageUtils.setCurrentPageNo(currentPageNo);
            pageUtils.setPageSize(pageSize);
            pageUtils.setTotalPageCount(totalPageCount);
            pageUtils.setTotalPageSize(totalPageSize);
            pageUtils.setList(studentList);
            req.setAttribute("pageUtils",pageUtils);
            req.getRequestDispatcher("index.jsp").forward(req,resp);
        }
    }
    

     

    index.jsp

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
      <head>
        <title>分页</title>
        <script src="${pageContext.request.contextPath}/js/jquery-1.8.3.js"></script>
      </head>
      <body>
    
    <%--  重定向,加载两次页面(跳过第一次无数据)--%>
    <%      Object object = request.getAttribute("pageUtils");
            if (object==null){
              request.getRequestDispatcher("studentServlet").forward(request,response);
            }
    %>
    
    
      <table border="1" width="700px" align="center">
        <tr>
          <td>编号</td>
          <td>名字</td>
          <td>年龄</td>
        </tr>
        <c:forEach items="${pageUtils.list}" var="student">
          <tr>
            <td>${student.uid}</td>
            <td>${student.uname}</td>
            <td>${student.uage}</td>
          </tr>
        </c:forEach>
    
        </table>
    
      <div style="margin-left: 320px">
        <a href="${pageContext.request.contextPath}/studentServlet?currentPageNo=1">首页</a>
    <c:if test="${pageUtils.currentPageNo>1}">
        <a href="${pageContext.request.contextPath}/studentServlet?currentPageNo=${pageUtils.currentPageNo-1}">上一页</a>
    </c:if>
    <c:if test="${pageUtils.currentPageNo < pageUtils.totalPageSize}">  <%--中间的小于,是在大括号内--%>
        <a href="${pageContext.request.contextPath}/studentServlet?currentPageNo=${pageUtils.currentPageNo+1}">下一页</a>
    </c:if>
        <a href="${pageContext.request.contextPath}/studentServlet?currentPageNo=${pageUtils.totalPageSize}">末页</a>
        <span>共有${pageUtils.totalPageCount}条</span>
        <span>${pageUtils.currentPageNo}页/${pageUtils.totalPageSize}页</span>
        <input type="text" id="tv_count" style=" 40px"><input type="button" value="go" id="tv_but">
      </div>
      <script>
        $("#tv_but").click(function () {
          var currentPageNo = $("#tv_count").val(); /* 获取 input 标签框的内容*/
          window.location.href = "${pageContext.request.contextPath}/studentServlet?currentPageNo="+currentPageNo;
        })
    
      </script>
      </body>
    </html>
    

     

    StudentDao (实现;定义)

     

    Student (实现;三个变量;两构造;set get;toString)

     

    PageUtils (五个变量;两构造;set get;toString)

     

    结果展示:

    首页

    第四页

    跳转成功

     

  • 相关阅读:
    LinkedBlockingQueue
    PriorityBlockingQueue
    js阻止事件冒泡
    java map常用的4种遍历方法
    JAVA jar 和 war 包的区别
    jquery 操作大全
    Java 使用new Thread和线程池的区别
    原生XMLHttpRequest
    socket 和 webscoket 的区别
    GET和POST请求的区别如下
  • 原文地址:https://www.cnblogs.com/ynzj123/p/13498635.html
Copyright © 2020-2023  润新知