关于用预编译写mysql的模糊查询语句问题
String sql = "select * from post where postTitle like ? order by isTop desc,publishTime desc limit ?,?";
pstmt.setString(1,"%"+what+"%");
pstmt.setInt(2, (pageIndex-1)*pageSize);
pstmt.setInt(3, pageSize);
下面是报错信息:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'postTitle like '%??‰?·?%'' at line 1
请问我要怎么改啊。。
[解决办法]
这个pstmt.setString(1,"%"+what+"%");
改成这个试试:
pstmt.setString(1,"'%"+what+"%'");