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: [%小%]
原文地址:https://www.cnblogs.com/chengjianJava/p/12207987.html
时间: 2024-10-10 14:46:20