当前位置: 代码迷 >> Java Web开发 >> 模糊查询时用到prepareStatement在SQL语句里的问号如何写
  详细解决方案

模糊查询时用到prepareStatement在SQL语句里的问号如何写

热度:3248   发布时间:2013-02-25 21:10:57.0
模糊查询时用到prepareStatement在SQL语句里的问号怎么写
我用了prepareStatement(sql)来防止SQL注入问题,出入的SQL语句参数全部用?代替的,但是我有用到模糊查询不知道模糊查询的SQL语句的参数怎么用?取代。
我是这么写的,但是查不到结果,那位高手可以指点一下啊,先谢了:
select * from book_info where book_name like '%?%' and book_type like'%?%' and book_author like '%?%' and book_publisher like '%?%'

------解决方案--------------------------------------------------------
要+连接字符串
select * from book_info where book_name like '%'+?+'%' and book_type like'%'+?+'%' and book_author like '%'+?+'%' and book_publisher like '%'+?+'%'

------解决方案--------------------------------------------------------
select * from book_info where book_name like '%"+?+"%' and book_type like'"%+?+"%' and book_author like '%"+?+"%' and book_publisher like '%"+?+"%'
上面这样写就OK了
记得要多结贴!
------解决方案--------------------------------------------------------
探讨
你这种方法通用性就不好了啊,必须要为这条语句另外写个查询方法了,我是想所有SQL语句都用同一个查询方法。