Java 执行包含in子句的sqlQuery时出错
我有一个sqlQuery:Java 执行包含in子句的sqlQuery时出错,java,mysql,Java,Mysql,我有一个sqlQuery: select * from products where product_type in :prodTypes query.setParameter("prodTypes",getInClauseFromList(productTypes)); productTypes是一个列表 我得到一个错误: Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in
select * from products where product_type in :prodTypes
query.setParameter("prodTypes",getInClauseFromList(productTypes));
productTypes是一个列表
我得到一个错误:
Caused by: 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 ''(\'clothes\')')
我建议使用类似Spring的JDBCTemplate。然后,您不必手动将列表转换为字符串 在上面的示例中,您传递的字符串被引用(应该是这样),因此生成的SQL是不正确的
假设我不知道“query”是哪种类型:尝试直接将列表设置为参数。大多数框架都应该支持为IN子句正确地转换它。如果您也能告诉我们查询的实际文本是什么,这将对您和我们都有帮助。现在,我想我们只能猜测确切的语法错误。我尝试了show sql:I get select*from products where product_type in?这没有帮助:(我需要将子句中的
看作纯文本。有一些小问题,但我们需要看到。好吧……但这有点像把婴儿连同洗澡水一起扔掉。我给你的建议是打印到控制台或记录getIncluseFromList()
的结果。很明显,MySQL为什么会在上面呕吐。
Caused by: 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 ''(\'clothes\')')