10.8

分页

public class findByPageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String current = request.getParameter("currentPage");
        String size1 =request.getParameter("size");
        int currentPage = Integer.parseInt(current);
        int size = Integer.parseInt(size1);
        IUserService userService = new UserService();
        PageInfo pageInfo = userService.findByPage(currentPage,size);
        HttpSession session=request.getSession();
        session.setAttribute("user1",pageInfo);
        request.getRequestDispatcher("allUser.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int current = 1;
        int size1 =5;
        IUserService userService = new UserService();
        PageInfo pageInfo = userService.findByPage(current,size1);
        HttpSession session=request.getSession();
        session.setAttribute("user1",pageInfo);
        request.getRequestDispatcher("allUser.jsp").forward(request, response);
    }
}
 public PageInfo findByPage(int currentPage, int size) {
        PageInfo pageInfo = new PageInfo();
        pageInfo.setCurrentPage(currentPage);
        pageInfo.setSize(size);
        int start =(currentPage-1)*size;
        List<User> users =userDao.findByPage(start,size);
        pageInfo.setList(users);
        int count = userDao.count();
        pageInfo.setTotalCount(count);
        Double c  =Double.valueOf(count);
        Double ceil = Math.ceil(c/size);
        pageInfo.setTotalPage(ceil.intValue());
        return pageInfo;
 public List<User> findByPage(int start, int size) {
        ResultSet resultSet = null;
        PreparedStatement statement = null;
        Connection connection = null;
        List<User> users = new ArrayList<>();
        try {
            connection = DBUtill.getConnection();
            String sql = "select * from fuser limit ?,?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,start);
            statement.setInt(2,size);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                User user = new User();
                user.setId(resultSet.getInt(1));
                user.setName(resultSet.getString(2));
                user.setPassword(resultSet.getString(3));
                users.add(user);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtill.closeAll(resultSet, statement, connection);
        }
        return users;
    }

    @Override
    public int count() {
        ResultSet resultSet = null;
        PreparedStatement statement = null;
        Connection connection = null;
        int count = 0;
        try {
            connection = DBUtill.getConnection();
            String sql = "SELECT COUNT(*) FROM fuser";
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()){
            count = resultSet.getInt(1);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtill.closeAll(resultSet, statement, connection);
        }
        return count;
    }

package com.gm.pojo;

import com.gm.Model.User;

import java.util.List;

public class PageInfo {
    private int currentPage;
    private int size;
    private List<User> list;
    private int totalPage;
    private int totalCount;

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public List<User> getList() {
        return list;
    }

    public void setList(List<User> list) {
        this.list = list;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totaLCount) {
        this.totalCount = totaLCount;
    }

    @Override
    public String toString() {
        return "PageInfo{" +
                "currentPage=" + currentPage +
                ", size=" + size +
                ", list=" + list +
                ", totalPage=" + totalPage +
                ", totaLCount=" + totalCount +
                '}';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值