码迷,mamicode.com
首页 > 其他好文 > 详细

Wrong number of parameters: expected 0, was given 1 Query: select count(*) from product where pname like '%?%' Parameters: [%小%]

时间:2020-01-18 00:43:09      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:clip   系统   方法   ber   HERE   个数   dao   rod   模糊查询   

eclipse在dao层写的模糊查询sql:String sql = "select count(*) from product where pname like ‘%?%‘";

这样写会导致以下错误

Wrong number of parameters: expected 0, was given 1 Query: select count(*) from product where pname like ‘%?%‘ Parameters: [%小%]<br/>

原因:

系统并没有把sql里面的语句当成占位符?,只是当成了普通的字符。

如果在其他sql遇到这样的报错,

检查sql需要的参数个数(expected 0)和传递的参数个数(was given 1)是否一致

解决方法:

将sql改成:String sql = "select count(*) from product where pname like ?";

然后将被占位的参数用 "%"+参数+"%"拼接作为模糊查询的条件<br/>

保证sql需要参数数量和给出的参数数量一致

Wrong number of parameters: expected 0, was given 1 Query: select count(*) from product where pname like '%?%' Parameters: [%小%]

标签:clip   系统   方法   ber   HERE   个数   dao   rod   模糊查询   

原文地址:https://www.cnblogs.com/chengjianJava/p/12207987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!