JSP实现数据库表数据分页显示并添加模糊匹配查询

7 篇文章 0 订阅

一、JSP实现数据库表数据分页显示大致有两种方式:

  1. 取出符合条件的数据,放在数据结构或内存中,然后按页数和每页显示的数量进行浏览。举个栗子,假如当前有50条数据,我们需要查看第二页,每页显示20条数据,则我们需要查看的是50条数据中第 (2 - 1) * 20 + 1 条到第 2 * 20 条数据,这个比较好理解;
  2. 需要多少数据取多少数据,在查询数据库时仅查询当前页数需要显示的数据,可以利用SQL语句如 limit 来实现。举个栗子,假如我们需要查看第二页,每页显示20条数据,则使用的SQL语句应该是 SELECT * FROM table LIMIT 21, 20;

二、实现模糊匹配查询:

  • 使用MySQL中的LOCATE语句实现模糊匹配查询功能;

如下代码使用了第一种方式实现分页功能以及模糊匹配查询功能:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/8/7
  Time: 16:38
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<html>
<head>
    <title>Fisherman 的主页</title>
</head>
<body>
<h1>使用 JSP 分页列出 emp 表数据</h1>
<%!
    class Employee {
        private int empno;
        private String ename;
        private String job;
        private String hiredate;
        private float sal;
        private float comm;

        Employee(int empno, String ename, String job, String hiredate, float sal, float comm) {
            this.empno = empno;
            this.ename = ename;
            this.job = job;
            this.hiredate = hiredate;
            this.sal = sal;
            this.comm = comm;
        }

        int getEmpno() {
            return empno;
        }

        String getEname() {
            return ename;
        }

        String getJob() {
            return job;
        }

        String getHiredate() {
            return hiredate;
        }

        float getSal() {
            return sal;
        }

        float getComm() {
            return comm;
        }
    }
%>
<%
    String URL = "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimeZone=UTC";
    String USERNAME = "root";
    String PASSWORD = "Root1234";
    String DRIVER = "com.mysql.jdbc.Driver";
%>
<%
    Connection conn = null;
    Statement stat = null;
    ResultSet rs = null;
%>
<%
    try {
        Class.forName(DRIVER);
        conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        stat = conn.createStatement();
        // get query name from request
        String rawString = request.getParameter("ename");
        String rawQuery;
        StringBuilder sql = new StringBuilder();
        if (rawString == null || rawString.equals("")) {
            sql.append("SELECT * FROM emp");
            rawQuery = "page_list_emp.jsp?";
        } else {
            String queryName = new String(rawString.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
            sql.append("SELECT * FROM emp WHERE LOCATE(").append("'").append(queryName).append("'").append(", ename) > 0");
            rawQuery = "page_list_emp.jsp?ename=" + queryName + "&";
        }
        rs = stat.executeQuery(sql.toString());
%>
<%
    List employeeList = new ArrayList();
    while (rs.next()) {
        int empno = rs.getInt(1);
        String ename = rs.getString(2);
        String job = rs.getString(3);
        Date hiredate = rs.getDate(4);
        float sal = rs.getFloat(5);
        float comm = rs.getFloat(6);
        employeeList.add(new Employee(empno, ename, job, hiredate.toString(), sal, comm));
    }
%>
<%
    int pageNum = employeeList.size();
    int pageSize = 5;

    String strNum = request.getParameter("pageNum");
    // get current page number
    int number;
    if (strNum == null || strNum.equals("0")) {
        number = 1;
    } else {
        number = Integer.parseInt(strNum);
    }
    int maxPage;
    if (pageNum % 5 == 0) {
        maxPage = pageNum / 5;
    } else {
        maxPage = pageNum / 5 + 1;
    }
    if (number > maxPage) {
        number = maxPage;
    }
    int start = (number - 1) * pageSize;
    int end = number * pageSize;
    if (end > pageNum) {
        end = pageNum;
    }
%>
<table border="1" width="80%">
    <tr align="center" valign="top">
        <td colspan="6">
            共<%=maxPage%>页&nbsp;共<%=pageNum%>条记录&nbsp;当前是第<%=number%>页&nbsp;
            <a href="<%=rawQuery%>pageNum=0">首页</a>&nbsp;
            <a href="<%=rawQuery%>pageNum=<%=number - 1%>">上一页</a>&nbsp;
            <a href="<%=rawQuery%>pageNum=<%=number + 1%>">下一页</a>&nbsp;
            <a href="<%=rawQuery%>pageNum=<%=maxPage%>">末页</a>
        </td>
    </tr>
    <tr align="center">
        <td colspan="6">
            <form action="page_list_emp.jsp" method="get">
                雇员姓名:<input type="text" name="ename" title=""/><input type="submit" value="查询"/>
            </form>
        </td>
    </tr>
    <tr>
        <td>雇员编号</td>
        <td>雇员姓名</td>
        <td>雇员职务</td>
        <td>雇员工资</td>
        <td>雇用日期</td>
        <td>雇员奖金</td>
    </tr>
    <%
        for (int j = start; j < end; j++) {
            Employee emp = (Employee) employeeList.get(j);
    %>
    <tr>
        <td><%=emp.getEmpno()%></td>
        <td><%=emp.getEname()%></td>
        <td><%=emp.getJob()%></td>
        <td><%=emp.getSal()%></td>
        <td><%=emp.getHiredate()%></td>
        <td><%=emp.getComm()%></td>
    </tr>
    <%
        }
    %>
</table>
<%
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (conn != null) {
                conn.close();
            }
            if (stat != null) {
                stat.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
%>
</body>
</html>

创建表的SQL脚本:

CREATE TABLE emp (
    empno INT AUTO INCREMENT NOT NULL PRIMARY KEY,
    ename VARCHAR(10) NOT NULL,
    job VARCHAR(9) NOT NULL,
    hiredate DATE NOT NULL,
    sal FLOAT NOT NULL,
    comm FLOAT NOT NULL
) ENGINE=INNODB CHARSET=UTF8;

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值