我正在尝试通过jdbc代码在带有MySQL 5.5的in参数中进行简单查询 [英] I am trying a simple query with in parameter with mysql 5.5 through jdbc code

查看:86
本文介绍了我正在尝试通过jdbc代码在带有MySQL 5.5的in参数中进行简单查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆