简体   繁体   English

使用带有 LIKE 的 Java 准备语句

[英]Use java prepared statement with LIKE

I want to use java prepared statement with LIKE keyword.我想使用带有 LIKE 关键字的 java 准备语句。 I'm trying to match a date string.我正在尝试匹配日期字符串。 Heres my code.继承人我的代码。

String summaryChooser = "SELECT * FROM PRODUCT p, CODE_COVERAGE_SUMMARY s WHERE "
                    + " p.product_id = s.product_id AND p.product_id = ? AND s.date LIKE ? "
                    + " ORDER BY s.date DESC LIMIT 1 ";
ps = dbConnection.prepareStatement(summaryChooser);
ps.setInt(1, productsResult.getInt("product_id"));
ps.setString(2,  "%" + date + "%");

It gives below exception.它给出了以下例外。

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 你的 SQL 语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near '?检查与您的 MySQL 服务器版本相对应的手册,了解在 '? AND s.date LIKE ?和 s.date 喜欢吗? ORDER BY s.date DESC LIMIT 1' at line 1 ORDER BY s.date DESC LIMIT 1' 在第 1 行

I could make it work by doing this.我可以通过这样做来使它工作。

ps.setString(2, date + "%");

Thanks everyone :)谢谢大家 :)

Replace:代替:

ps.setString(2,  "%" + date + "%");

with:和:

ps.setString(2,  "'" + date + "'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM