jsp分页技术

jsp分页技术

在实际应用中,如果从数据库中查询的记录特别的多,甚至超过了显示屏的显示范围,这个时候可将结果进行分页显示。

分页首先要确定有多少条记录然后根据一页要显示几行来进行分页里面包含jsp,java等

下面是代码可以拿来用

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%> 
<html>
 <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
     $("#jia").color(red)
     </script>
<head>
<meta charset="UTF-8">
<title>查询页</title>
</head>
<body>
<table >
<tr id = "jia">
<td>编号</td>
<td>账户名</td>
<td>密码</td>
<td>手机号 </td>
<td>性别</td>
<td>身份证号</td>
</tr>
<c:forEach items="${caxun }" var="l">
<tr>
<td>${l.uid }</td>
<td>${l.username }</td>
<td>${l.pwd }</td>
<td>${l.tel } </td>
<td>${l.sex }</td>
<td>${l.idcard } </td>
</tr>
</c:forEach>
<tr>
<td><a href="Kong?pageCode=1">首页</a></td>
<c:if test="${pageCode==1}" var="c"></c:if>
<c:if test="${c}">
<td><a href="Kong?pageCode=${pageCode=1}">上一页</a></td>
</c:if>
<c:if test="${!c}">
<td><a href="Kong?pageCode=${pageCode-1}">上一页</a></td>
</c:if>
<td>当前是${pageCode}页</td>
<td>共${totalCode}页</td>
<td><a href="Kong?pageCode=${pageCode+1}">下一页</a></td>
<td><a href="Kong?pageCode=${totalCode}">尾页</a></td>

</tr>
</table>
</body>
</html>

后台代码

package com.hp.de;

import java.io.IOException;
import java.util.List;
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 com.hp.bean.User;
import com.hp.dao.UserDao;
import com.hp.dao.UserDaoImpl;

@WebServlet("/Kong")
public class Kong extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        int pageSize = 3; int pageCode = 0; int totalCode=0; int totalCount=0;
         UserDao dao = new UserDaoImpl();
         totalCount = dao.shu();
         if (totalCount%pageSize==0) {
             totalCode = totalCount/pageSize;
        }else {
            totalCode = totalCount/pageSize+1;
        }
        if (request.getParameter("pageCode")==null || request.getParameter("pageCode")=="") {
            pageCode = 1;
        }else {
            if (Integer.parseInt(request.getParameter("pageCode"))>totalCode) {
                pageCode = totalCode;
            }else {
                pageCode=Integer.parseInt(request.getParameter("pageCode"));
            }
        }
         request.setAttribute("pageCode", pageCode);
         request.setAttribute("totalCode", totalCode);
         List<User> caxun = dao.caxun(pageSize,Integer.parseInt(request.getParameter("pageCode")));
         request.setAttribute("caxun", caxun);
         request.getRequestDispatcher("Fen.jsp").forward(request, response);
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

链接数据库

public class UserDaoImpl implements UserDao{
       String className = "com.mysql.cj.jdbc.Driver";
       String url = "jdbc:mysql://localhost:3306/mode?serverTimezone=UTC";
       String user = "root";
       String password = "123";
    @Override
    public int shu() {
        int a = 0;
        try {
            Class.forName(className);
             Connection con = DriverManager.getConnection(url, user, password);
             String s = "SELECT count(*) as 'shu' FROM user";
             PreparedStatement pr = con.prepareStatement(s);
             ResultSet ex = pr.executeQuery();
             if (ex.next()) {
                a= ex.getInt("shu");
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
        return a;
    }
    @Override
    public List<User> caxun(int pageSize ,int pageCode ) {
         List<User> li = new ArrayList<User>();
        try {
            Class.forName(className);
             Connection con = DriverManager.getConnection(url, user, password);
             String s = "SELECT * FROM user limit ?,?";
             PreparedStatement pr = con.prepareStatement(s);
             pr.setInt(1,(pageCode-1)*pageSize);
             pr.setInt(2,pageSize);
             ResultSet ex= pr.executeQuery();
            while (ex.next()) {
                User us = new User();
                us.setUid(ex.getInt("uid"));
                us.setUsername(ex.getString("username"));
                us.setPwd(ex.getString("pwd"));
                us.setSex(ex.getString("sex"));
                us.setTel(ex.getString("tel"));
                us.setIdcard(ex.getString("idcard"));
                li.add(us);
            }
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return li;
    }

上一篇:linux – ELF文件可以包含多个符号表吗?


下一篇:linux – 共享库中的重定位条目