我正在尝试通过jdbc代码在带有MySQL 5.5的in参数中进行简单查询 [英] I am trying a simple query with in parameter with mysql 5.5 through jdbc code
问题描述
String sql="select * from matrix where metric_name in(?)";
PreparedStatement st1 = conn.prepareStatement(sql);
st1.setString(1,"NO_OF_ORDERS");
resultSet = st1.executeQuery(sql);
while (resultSet.next()) {
}
相同的查询在mysql 5.7上可以正常使用,但是我找不到问题. 如何从jdbc传递mysql 5.5中的参数
The same query works fine with mysql 5.7 but I am not able to find the issue. How do pass in parameters in mysql 5.5 from jdbc
错误是: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException:您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以在第1行的'?)'附近使用正确的语法
Error is : com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 '?)' at line 1
推荐答案
您不应在PreparedStatement
上调用executeQuery(String)
,而应调用executeQuery()
. JDBC api明确禁止这样做,但仅由于MySQL Connector/J偶然(由于语法错误)而出错.
You should not call executeQuery(String)
on a PreparedStatement
, you should call executeQuery()
instead. This is explicitly disallowed by the JDBC api, but only goes wrong by accident (due to the syntax error) with MySQL Connector/J.
请注意,您当前的查询等同于:
Note that your current query is the equivalent of:
select * from matrix where metric_name = ?
我没有看到您在此处使用IN
的原因.
I don't see a reason why you'd use IN
here.
这篇关于我正在尝试通过jdbc代码在带有MySQL 5.5的in参数中进行简单查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!