【问题标题】:Using "like" wildcard in prepared statement在准备好的语句中使用“like”通配符
【发布时间】:2012-01-05 02:04:15
【问题描述】:

我正在使用准备好的语句来执行 mysql 数据库查询。我想实现基于某种关键字的搜索功能。

为此,我需要使用 LIKE 关键字,我知道的就这么多。而且我之前也使用过准备好的语句,但我不知道如何将它与LIKE 一起使用,因为从下面的代码中我将在哪里添加'keyword%'

我可以直接在pstmt.setString(1, notes) 中使用它作为(1, notes+"%") 或类似的东西。我在网上看到很多关于此的帖子,但在任何地方都没有好的答案。

PreparedStatement pstmt = con.prepareStatement(
      "SELECT * FROM analysis WHERE notes like ?");
pstmt.setString(1, notes);
ResultSet rs = pstmt.executeQuery();

【问题讨论】:

    标签: java mysql jdbc prepared-statement


    【解决方案1】:

    您需要在值本身中设置它,而不是在准备好的语句 SQL 字符串中。

    所以,这应该适用于前缀匹配:

    notes = notes
        .replace("!", "!!")
        .replace("%", "!%")
        .replace("_", "!_")
        .replace("[", "![");
    PreparedStatement pstmt = con.prepareStatement(
            "SELECT * FROM analysis WHERE notes LIKE ? ESCAPE '!'");
    pstmt.setString(1, notes + "%");
    

    或后缀匹配:

    pstmt.setString(1, "%" + notes);
    

    或全局匹配:

    pstmt.setString(1, "%" + notes + "%");
    

    【讨论】:

    • +1 OP 可以在 SQL 中“设置”它——如... LIKE '%' || ? || '%' 或类似的——但这不太灵活。
    • 如何使用非大小写敏感模式? :)
    • 在使用pstmt.setString(2, "%" + notes + "%")时,不区分大小写仍然可以使用WHERE UPPER(?) LIKE UPPER(?)
    • @Alain:谢谢。只是想知道,这是否适用于全世界都知道的所有 RDBMS?毕竟,也许第一条评论中提到的'%' || ? || '%' 更好?我现在没有机会进行实验。
    • @BalusC 这适用于我的测试中的 MSSQL、Postgres 和 MySQL。被制成参数的字符串本身被解释为数据和控制指令的混合。 SQL 连接发生在它被解释并保留漏洞之前。 IEEE 安全设​​计中心告诉Strictly Separate Data and Control Instructions, and Never Process Control Instructions Received from Untrusted Sources
    【解决方案2】:

    这样编码:

    PreparedStatement pstmt = con.prepareStatement(
        "SELECT * FROM analysis WHERE notes like ?");
    pstmt.setString(1, notes + "%");`
    

    确保您不要像下面那样包含引号'',因为它们会导致异常。

    pstmt.setString(1,"'%"+ notes + "%'");
    

    【讨论】:

    • 虽然听起来好像有人不会遇到这种假设,但它实际上非常有效,尤其是在使用 Oracle 时。谢谢指出!
    【解决方案3】:

    我们可以使用CONCAT SQL 函数。

    PreparedStatement pstmt = con.prepareStatement(
          "SELECT * FROM analysis WHERE notes like CONCAT( '%',?,'%')";
    pstmt.setString(1, notes);
    ResultSet rs = pstmt.executeQuery();
    

    这非常适合我的情况。

    【讨论】:

    • 伙计,你拯救了我的一天:D
    • 也拯救了我的一天。谢谢。
    【解决方案4】:
    PreparedStatement ps = cn.prepareStatement("Select * from Users where User_FirstName LIKE ?");
    ps.setString(1, name + '%');
    

    试试这个。

    【讨论】:

      【解决方案5】:
      String fname = "Sam\u0025";
      
      PreparedStatement ps= conn.prepareStatement("SELECT * FROM Users WHERE User_FirstName LIKE ? ");
      
      ps.setString(1, fname);
      

      【讨论】:

      【解决方案6】:
      String query="select * from test1 where "+selected+" like '%"+SelectedStr+"%';";
      
      
      PreparedStatement preparedStatement=con.prepareStatement(query);
      
      
      // where seleced and SelectedStr are String Variables in my program
      

      【讨论】:

      • 不安全,请使用参数化preparedstatement。
      猜你喜欢
      • 1970-01-01
      • 2014-09-04
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      相关资源
      最近更新 更多