在搜索栏中使用单引号

Using single quotes in a search bar

我试图在我的 SQL 数据库中搜索条目,但每当我在搜索栏中输入单引号并 运行 它时,我都会收到错误消息。当我键入特定名称时,它 returns 来自数据库的正确条目。我读过有关使用 ''、转义和使用准备好的语句的信息,但是 none 的答案都适合我的情况,或者我在概念上不理解某些东西。 (我是初学者)提前致谢! :)

下面是我的 jsp 文件的代码,当我 运行 时它是什么样子,以及当我插入单引号时会发生什么。

<%@page import="java.sql.*"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>

<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>List of Entries</title>
</head>
<body>
    <h1>Retrieve Data</h1>
    <form method="post" action="index.jsp">
        Search <input type="text" name="search"
            placeholder="Enter your search here..." /> 
            <input type="submit"
            value="Search" name="submit"/>
    </form><br><br>
    <%
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        ResultSet rs2 = null;


        try {
            String searchReq = request.getParameter("search");
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            //make connection to db
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/profile", "root", "");

            //create a statement
            stmt = con.createStatement();

            //executes SQL query
            rs = stmt.executeQuery("SELECT * FROM person WHERE Name LIKE '%"+ searchReq +"%'");
    %>

    <table border="1">
        <tr>
            <td><b>Name</b></td>
            <td><b>Birthday</b></td>
            <td><b>Address</b></td>
            <td><b>Gender</b></td>
            <td><b>School</b></td>
        </tr>

        <%
            try {
                    while (rs.next()) {
        %>

        <tr>
            <td><%=rs.getString("Name")%></td>
            <td><%=rs.getString("Birthday")%></td>
            <td><%=rs.getString("Address")%></td>
            <td><%=rs.getString("Gender")%></td>
            <td><%=rs.getString("School")%></td>
        </tr>

        <%
            }
                } catch (Exception e) {
                    e.printStackTrace();
                }
        %>

    </table>
    <%
        }

        catch (Exception e) {
            e.printStackTrace();
        }

        finally {

            rs.close();
            stmt.close();
            con.close();

        }
    %>
</body>
</html>

jsp page with search bar

error page

你的主要问题在这里:

"SELECT * FROM person WHERE Name LIKE '%"+ searchReq +"%'"

当变量 searchReq 包含单引号时,您正在创建 sql 查询:

SELECT * FROM person WHERE Name LIKE '%'%'
                                       ^ sql engine consider this as end of string literal

当数据库尝试解析此查询时,它找到正确的部分 SELECT * FROM person WHERE Name LIKE '%',然后找到无效的部分 %'

这和纯java中的一样,如果你想声明里面有"的字符串,如果你尝试:

String s = "my string with " quote";
                           ^ java considers this as end of string literal

你会得到语法错误信息,你需要转义这样的符号,对于java它是\"

因此,要解决此问题,您需要使用参数查询,或检查 sql standard:

<character representation> ::= <nonquote character> | <quote symbol>

<quote symbol> ::= <quote><quote>

所以,单引号应该是 present/escaped 作为双单引号 ''

因此,如果您将所有 ' 替换为 '' - 您将摆脱 sql 注入并修复失败的查询。

最简单的方法是searchReq = searchReq.replaceAll("'", "''");

但是,sql 运算符 LIKE 有 2 个额外的特殊符号 %_

If an <escape character> is not specified, then each <underscore> character in P represents an arbitrary character specifier, each <percent> character in P represents an arbitrary string specifier, and each character in P that is neither the <underscore> character nor the <percent> character represents itself.

考虑一下如果用户输入这些符号,您的查询会找到什么?

提示:看标准:

<like predicate> ::= <match value> [ NOT ] LIKE <pattern> [ ESCAPE <escape character> ]