String select = "select script from kit where (name = ?) and (creator = ?)";
PreparedStatement prepared = db.prepareStatement(select);
prepared.setString(1, name);
prepared.setInt(2, creator);
ResultSet result = prepared.executeQuery(select);
给我一个“SQL 语法错误”
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 '?) and (creator = ?)' at line 1
发生什么事了?我该如何修复它?
最佳答案
我的猜测是 JDBC 或您的底层数据库不喜欢将 WHERE
子句中的术语放在括号中。尝试删除它们:
String select = "select script from kit where name = ? and creator = ?";
PreparedStatement prepared = db.prepareStatement(select);
prepared.setString(1, name);
prepared.setInt(2, creator);
ResultSet result = prepared.executeQuery();
另请注意,PreparedStatement#executeQuery()
不带任何参数。由于您已经准备了语句并绑定(bind)了参数,因此您只需执行该查询即可。
令我惊讶的是 MySQL 对此有所提示,因为从我在本地测试中看到的情况来看,您当前的查询执行应该没有任何问题。
关于java - 使用预准备语句的 SQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54423409/