在'?'附近使用正确的语法
我有一个Java代码:
String searchPerson = "select * from persons where surname like ? and name like ?"; //connect to DB PreparedStatement statement = connect.prepareStatement(searchPerson); statement.setString(1,"%"+ surname + "%"); statement.setString(2, "%" + name + "%"); ResultSet resultPerson = statement.executeQuery(searchPerson); //..code
然后我有SQLException:
你的SQL语法有错误; 检查与您的MySQL服务器版本相对应的手册,在“?”附近使用正确的语法。
您应该执行不带参数的PrepareStatement
,如下所示:
statement.executeQuery()
使用String
参数调用executeQuery
将按原样执行提供的查询(不带绑定参数)。
ResultSet resultPerson = statement.executeQuery(searchPerson);
应该
ResultSet resultPerson = statement.executeQuery();
试试statement.setString(1,"'%"+ surname + "%'");