数据库查询中,我们是这样进行模糊匹配的:
select * from user where 1=1 and username like '%林%' limit 0,20
理所当然的,在java代码中你会这么写:
select * from user where 1=1 and username like '%?%' limit 0,20
ps.setString(1, userCondition.getUsername());
如果真这么写,你就等着扑街吧:Parameter index out of range (1 > number of parameters, which is 0)
这时候怎么破?
select * from user where 1=1 and username like ? limit 0,20
ps.setString(1, "%"+userCondition.getUsername()+"%");
PreparedStatement 模糊匹配 结果却:Parameter index out of range (1 > number of parameters, which is 0)
最新推荐文章于 2024-07-16 13:42:24 发布
本文介绍了一个常见的Java数据库查询陷阱:如何正确地使用参数化查询进行模糊搜索。文章通过一个具体的例子解释了错误做法可能导致的问题,并给出了正确的解决方案。
982

被折叠的 条评论
为什么被折叠?



