java中关于模糊查询:String stmt = "select * from admin where username like '%?%'";
报错:{"status":"error","message":"(数据库)PreparedStatementCallback; SQL [select * from admin where username like \u0027%?%\u0027]; Parameter index out of range (1 \u003e number of parameters, which is 0).; nested exception is java.sql.SQLException: Parameter index out of range (1 \u003e number of parameters, which is 0)."}
出错原因:占位符(?)不能被单引号包围
请问:该怎么修改???
占位符(?)不能被单引号包围问题
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
5条回答 默认 最新
KiterCher 2017-04-10 05:05关注给你提两点建议:
1、sql语句不止可以使用占位符的,还有这种啊,其实安全效果都一样: "select * from admin where username like '%"+userName+"%' ";
2、如果你必须使用占位符的话 可以这样 :"select * from admin where username like ?"; setString(1,"%华%"); 这样是没问题的本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用 1