web分页

DAO

public interface studentDao {
    //查询数据总数
    public int getTotalNum() throws Exception;
    //返回当前页的数据
    public List<Student> getTotal(int cpage,int pageNum) throws Exception;
}

package Dao.daoImpl;

import Dao.Idao.studentDao;
import entity.Student;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class studentDaoImpl implements studentDao {

    public static  Connection con() throws ClassNotFoundException, SQLException {
        System.out.println("进入数据库连接");
        Class.forName("com.mysql.cj.jdbc.Driver") ;
        //创建数据库连接
        String url = "jdbc:mysql://localhost:3306/tese?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC" ;
        String username = "root" ;
        String password = "yfy57320" ;
        Connection connection= DriverManager.getConnection(url , username , password ) ;
        return connection;
    }

    @Override
    public int getTotalNum() throws Exception{
        System.out.println("进入dao1");
        //jdbc查询
            ResultSet rs=null;
            PreparedStatement stmt=null;
            Connection connection=studentDaoImpl.con();
            int all=-1;
            try {
                System.out.println("进入try1执行连接");
                //查询
                String sql = "select count(*) as num from Student";
                stmt = connection.prepareStatement(sql);
                rs = stmt.executeQuery();
                while (rs.next()) {
                    all = rs.getInt("num");
                }
            }
            finally {
                rs.close();
                stmt.close();
                connection.close();
            }
            System.out.println("dao查询到总数为"+all);
            return all;
    }

    @Override
    public List<Student> getTotal(int cpage,int pageNum) throws Exception {
        System.out.println("进入dao2");
        //jdbc查询
        ResultSet rs=null;
        PreparedStatement stmt=null;
        Connection connection=studentDaoImpl.con();
        List<Student> Students=new ArrayList<>();
        try{
            System.out.println("进入try2执行连接");
            //查询
            String sql="select *  from Student limit ?,? ";
            stmt=connection.prepareStatement(sql);
            stmt.setInt(1,(cpage-1)*3);
            stmt.setInt(2,pageNum);
            rs=stmt.executeQuery();
            while(rs.next()){
               int id=rs.getInt("id");
               String name =rs.getString("name");
               int sno=rs.getInt("sno");
               Student student=new Student(id,name,sno);
               Students.add(student);
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }finally {
            rs.close();
            stmt.close();
            connection.close();
        }
        System.out.println("dao查询到了数据"+Students);
        return Students;
    }

}

service

package Service.Iservice;

import entity.Student;

import java.util.List;

public interface studentService {
    public int getStuNum() throws Exception;
    public List<Student> getStu(int cpage,int pageNum) throws Exception;
}

package Service.serviceImpl;

import Dao.daoImpl.studentDaoImpl;
import Service.Iservice.studentService;
import entity.Student;

import java.util.List;

public class studentServiceImpl implements studentService  {
    studentDaoImpl studentDao=new studentDaoImpl();
    @Override
    public int getStuNum() throws Exception {
        System.out.println("执行service的查询数量方法");
            return studentDao.getTotalNum();

    }

    @Override
    public List<Student> getStu(int cpage,int pageNum) throws Exception {
        System.out.println("执行service的查询数据方法");
        return studentDao.getTotal(cpage,pageNum);
    }


}

servlet

package servlet;

import Service.serviceImpl.studentServiceImpl;
import entity.Student;
import entity.pages;

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("/student")
public class stuServlet extends HttpServlet{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        studentServiceImpl ss=new studentServiceImpl();
        pages p=new pages();
        int cpage;
        //获取要查询的页数
        if(req.getParameter("cpage")==null){
             cpage=1;//初次进入就是第一页
        }else{
             cpage=Integer.parseInt(req.getParameter("cpage"));
        }

        //设置了当前页数
        p.setCpage(cpage);
        System.out.println("当前页数:"+p.getCpage());

        //查询学生数量

        int StuNum= 0;
        try {
            System.out.println("进入第一个try");
            StuNum = ss.getStuNum();
            System.out.println("函数获取人数:"+ss.getStuNum());
        } catch (Exception e) {
            System.out.println("第一个有错");
            e.printStackTrace();
        }




        p.setAllStudent(StuNum);
        System.out.println("总人数设置"+p.getAllStudent());
        //设置每页大小为3
        p.setPageNum(3);
        //查询
        List<Student> pageStu = null;
        try {
            System.out.println("进入第二个try");
             pageStu=ss.getStu(p.getCpage(),3);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("第二个有错");
        }





            p.setPageStu(pageStu);
        req.setAttribute("p",p);
        System.out.println("p="+p.toString());
        req.getRequestDispatcher("/a.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }


}

jsp

<%@ page import="entity.Student" %>
<%@ page import="entity.pages" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: sunseeker
  Date: 2020/3/11
  Time: 15:59
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>Title</title>
</head>
<body>
<table>


<tr>
    <td>id</td>
    <td>name</td>
    <td>sno</td>
</tr>
<%
   pages pp= (pages) request.getAttribute("p");
     List<Student> AllStu=pp.getPageStu();
    if(AllStu!=null){
        for(Student s:AllStu){
%>
<tr>
    <td><%=s.getId()%></td>
    <td><%=s.getName()%></td>
    <td><%=s.getSno()%></td>
</tr>
<%
        }
    }else{

%>
   数据为空
<%
    }
%>
    <tr>
        <td><a href="student">首页</a></td>
        <td><a href="student?cpage=<%=pp.getCpage()-1%>">上一页</a></td>
        <td><%=pp.getCpage()%>/<%=pp.getAllPages()%></td>
        <td><a href="student?cpage=<%=pp.getCpage()+1%>">下一页</a></td>
        <td><a href="student?cpage=<%=pp.getAllPages()%>">尾页</a></td>
    </tr>

</table>

</body>
</html>

```## 其他

package entity;

import java.util.List;

public class pages {
//总页数
private int AllPages;
//数据数量
private int AllStudentNum;
//每页的大小
private int pageNum;
//当前页数
private int cpage;

//当前页的内容
private List<Student> pageStu;

public pages() {

}

public int getAllPages() {
    return AllPages;
}

public int getAllStudent() {
    return AllStudentNum;
}

public void setAllStudent(int allStudent) {
    AllStudentNum = allStudent;
}

public int getPageNum() {
    return pageNum;
}

public void setPageNum(int pageNum) {
    this.pageNum = pageNum;
    if(this.AllStudentNum%this.pageNum!=0){
        this.AllPages=this.AllStudentNum/this.pageNum+1;
    }else{
        this.AllPages=this.AllStudentNum/this.pageNum;
    }

}

public int getCpage() {
    return cpage;
}

public void setCpage(int cpage) {
    this.cpage = cpage;
}

public List<Student> getPageStu() {
    return pageStu;
}

public void setPageStu(List<Student> pageStu) {
    this.pageStu = pageStu;
}

@Override
public String toString() {
    return "pages{" +
            "AllPages=" + AllPages +
            ", AllStudentNum=" + AllStudentNum +
            ", pageNum=" + pageNum +
            ", cpage=" + cpage +
            ", pageStu=" + pageStu +
            '}';
}

}


package entity;

public class Student {
private int id;
private String name;
private int sno;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getSno() {
    return sno;
}

public void setSno(int sno) {
    this.sno = sno;
}

public Student(int id, String name, int sno) {
    this.id = id;
    this.name = name;
    this.sno = sno;
}
public Student() {

}

@Override
public String toString() {
    return "Student{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", sno=" + sno +
            '}';
}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值