简体   繁体   English

Java + MySQL-语法错误,但语句正确

[英]Java + MySQL - Syntax error but the statement is correct

My program executes INSERT query. 我的程序执行INSERT查询。 When I run it, I get an error 当我运行它时,出现错误

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:SQL语法有错误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO words(lang1, lang2, category, lang2_code, user) SELECT 'ahoj', 'hell' at line 1 检查与您的MySQL服务器版本对应的手册以获取正确的语法,以在“ INSERT INTO words(lang1,lang2,category,lang2_code,user)”附近使用SELECT“ ahoj”,“ hell”(第1行)

I tried to print the actual statement to the stdout: 我试图将实际语句打印到标准输出:

SET @lang:='Angličtina', @categ:='Nová';
INSERT INTO words(lang1, lang2, category, lang2_code, user)
SELECT 'ahoj', 'hello', c.id, l.id, 1 FROM categories c, languages l
WHERE c.name = @categ AND l.name = @lang;

As you can see, the statement is altered in the log. 如您所见,该语句在日志中已更改。 'hell' instead of 'hello' . 'hell'代替'hello' When I copy that into the mysql command line and execute, it works just fine so I assume the problem is in the JDBC somewhere. 当我将其复制到mysql命令行并执行时,它可以正常工作,因此我认为问题出在JDBC中。

That's not one statement. 那不是一个陈述。 If you want to use more than one statement at a time, don't use a PreparedStatement but for example addBatch but it seems that here you should simply issue 2 JDBC statements, one after the other. 如果您想一次使用多个语句,请不要使用PreparedStatement而要使用addBatch这样的语句,但是似乎您应该在这里简单地发出2条JDBC语句,一个又一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM