查询数据库,并将查询的数据进行分页

package pojo;

public class User {
    private int id;
    private String name;
    private String pwd;
    private String realname;

    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 String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }

    public User(int id, String name, String pwd, String realname) {
        this.id = id;
        this.name = name;
        this.pwd = pwd;
        this.realname = realname;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", pwd='" + pwd + '\'' +
                ", realname='" + realname + '\'' +
                '}';
    }

    public User() {
    }
}
package DB;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtils {
    public static final String URL="jdbc:mysql://127.0.0.1/fx01";
    public static final String NAME="root";
    public static final String PWD="root";
    public static Connection conn;
    public static Connection getConn(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            try {
                conn= DriverManager.getConnection(URL,NAME,PWD);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static  void realease(Connection conn){
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }


}
package dao;

import pojo.User;

import java.util.List;

public interface UserDao {
    public List<User> findAll(int pageSize,int pageCode);
    public int getCount();
}
package dao.impl;

import DB.DBUtils;
import dao.UserDao;
import pojo.User;

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

public class UserDaoImpl implements UserDao {
    

    @Override
    public List<User> findAll(int pageSize, int pageCode) {//pageCode当前页数
        List<User> list = new ArrayList<User>();
        Connection conn = DBUtils.getConn();
        String sql="select * from user limit ?,?";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,(pageCode-1)*pageSize);
            ps.setInt(2,pageSize);
            ResultSet rs = ps.executeQuery();
            while (rs.next()){
                User us = new User(
                        rs.getInt(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4)
                );
                list.add(us);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public int getCount() {
        int result=0;
        Connection conn = DBUtils.getConn();
        String sql="select count(*) from user";
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                result=rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return result;
    }
}
package servlet;

import dao.UserDao;
import dao.impl.UserDaoImpl;
import pojo.User;

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("/find")
public class UserSelectServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        int pageSize=3;//每个分页的行数
        int pageCode=0;//当前页数
        int totalCount=0;//总行数
        int totalCode=0;//总页数
        UserDao uid = new UserDaoImpl();
        totalCount=uid.getCount();
        //计算总页数
        if(totalCount%pageSize==0){
            //总行数除以每页的行数得到总页数
            totalCode=totalCount/pageSize;
        }else {
            //总行数除以每页的行数得到总页数,余数不为零页数就加1
            totalCode=totalCount/pageSize+1;
        }
        //判断用户是不是第一次访问页面,
        if(request.getParameter("pageCode")==null||request.getParameter("pageCode")==""){
            //为null就是第一次,那么页数就是从第一页开始
            pageCode=1;
        }else {
            //不是第一次访问,如果你访问的页数大于总页数,那么页数就是最后一页
            if(Integer.parseInt(request.getParameter("pageCode"))>totalCode){
                pageCode=totalCode;
            }else {
                //继承上次访问的页数
                pageCode=Integer.parseInt(request.getParameter("pageCode"));
            }
        }

        request.setAttribute("totalCode",totalCode);
        request.setAttribute("pageCode",pageCode);
        List<User> list= uid.findAll(pageSize,(Integer)request.getAttribute("pageCode"));
        request.setAttribute("list",list);
        request.getRequestDispatcher("info.jsp").forward(request,response);
    }
}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<table>
    <tr>
        <td>编号</td>
        <td>用户名</td>
        <td>密码</td>
        <td>真实姓名</td>
    </tr>
    <c:forEach items="${list }" var="user">
    <tr>
        <td>${user.id }</td>
        <td>${user.name }</td>
        <td>${user.pwd }</td>
        <td>${user.realname }</td>
    </tr>
    </c:forEach>
    <tr>
        <td><a href="find?pageCode=1">第一页</a></td>
            <c:if test="${pageCode==1 }" var="flag"></c:if>
            <c:if test="${flag}">
                <td><a href="find?pageCode=1">上一页</a></td>
            </c:if>
            <c:if test="${!flag}">
                <td><a href="find?pageCode=${pageCode-1 }">上一页</a></td>
            </c:if>
        <td>当前第${pageCode}页</td>
        <td><a href="find?pageCode=${pageCode+1 }">下一页</a></td>
        <td>总共${totalCode }页</td>
        <td><a href="find?pageCode=${totalCode }">最后一页</a></td>
    </tr>
</table>
</body>
</html>

需要导入mysql、standard、jstl等包

地址是从127.0.0.1:8080/项目名/find  进入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值