简体   繁体   English

将LIKE中的单引号插入JDBC准备的语句中

[英]Single quotes in LIKE into JDBC prepared statement

I'm implementing a search using a keyword over database rows. 我正在使用数据库行上的关键字来实现搜索。 I have to execute this query using a JDBC prepared statement. 我必须使用JDBC准备语句执行此查询。

SELECT id, name, ci FROM clients WHERE LOWER(name) LIKE '%keyword%'

What i tried was: 我试过的是:

ps = getConnection().prepareStatement("SELECT id, 
                                              name, 
                                              ci 
                                       FROM clientes 
                                       WHERE LOWER(name) LIKE '%?%'");

If i write this, the system find rows that have the ? 如果我写这个,系统会查找具有?的行。 symbol at start or end. 开头或结尾的符号。 And obviously the below statement fails saying that the query doesn't have an argument to pass to. 显然,以下语句未能说明查询没有要传递的参数。

ps.setString(1, keyword);

Same way with: 与以下方式相同:

SELECT id, nombre, ci FROM clientes WHERE LOWER(nombre) LIKE %?%

Any help is very appreciated 任何帮助都非常感谢

尝试使用如下形式的SQL文本:

... LIKE CONCAT('%', ? , '%')

Your prepared statement should be 您准备好的对帐单应为

SELECT id, name, ci FROM clients WHERE LOWER(name) LIKE ?

then you can set the parameter to %keyword% . 然后可以将参数设置为%keyword%

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

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