英文:
Got an exception! You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax
问题
我在使用Java和JavaFX,连接到MySQL数据库时遇到了这个错误。在将这个语句从Java执行到MySQL时,我得到了以下错误,请帮忙解决:
发生了异常!
> 在您的SQL语法中有一个错误;请检查与您的MySQL服务器版本相对应的手册,以获取正确的语法使用方式
> 在 'update kstds.match SET
> kstds.match.Team1Goals=kstds.match.Team1Goals+1 where kst' 处有错误,位于第1行
[![在这里输入图片描述][1]][1]
String Query ="use kstds; update kstds.match SET kstds.match.Team1Goals=kstds.match.Team1Goals+1 "
+ "where kstds.match.Team1ID= ( select kstds.team.TeamID from kstds.team where kstds.team.Name='AHLI' ) "
+ "and kstds.match.Matchid = 1 ; "
+ "Update kstds.match SET kstds.match.Team2Goals=kstds.match.Team2Goals+1 "
+ "where kstds.match.Team2ID= ( select kstds.team.TeamID from kstds.team where kstds.team.Name='AHLI' ) "
+ "and kstds.match.Matchid=1;";
[![在这里输入图片描述][2]][2]
[1]: https://i.stack.imgur.com/VYDqX.png
[2]: https://i.stack.imgur.com/esbeM.png
英文:
I got this error i am using java and javafx and it is connected to MYsql DB i got this error while excute this statment from java to my sql please help
Got an exception!
> 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 'update kstds.match SET
> kstds.match.Team1Goals=kstds.match.Team1Goals+1 where kst' at line 1
String Query ="use kstds; update kstds.match SET kstds.match.Team1Goals=kstds.match.Team1Goals+1 "
+ "where kstds.match.Team1ID= ( select kstds.team.TeamID from kstds.team where kstds.team.Name='AHLI' ) "
+ "and kstds.match.Matchid = 1 ; "
+ "Update kstds.match SET kstds.match.Team2Goals=kstds.match.Team2Goals+1 "
+ "where kstds.match.Team2ID= ( select kstds.team.TeamID from kstds.team where kstds.team.Name='AHLI' ) "
+ "and kstds.match.Matchid=1;";
答案1
得分: 1
你正在尝试执行多个 SQL 查询,应该使用 addBatch
和 executeBatch
进行操作。
你不需要执行 use kstds
,因为数据库连接是通过 Java 设置的。
试试这个:
String Query1 = "update match SET match.Team1Goals=match.Team1Goals+1 "
+ "where match.Team1ID= ( select team.TeamID from team where team.Name='AHLI' ) "
+ "and match.Matchid = 1 ; ";
String Query2 = "Update match SET match.Team2Goals=match.Team2Goals+1 "
+ "where match.Team2ID= ( select team.TeamID from team where team.Name='AHLI' ) "
+ "and match.Matchid=1;";
//stmt 是你的 Statement,conn 是你的 Connection
con.setAutoCommit(false);
stmt.addBatch(Query1);
stmt.addBatch(Query2);
stmt.executeBatch();
con.commit();
英文:
you are trying to execute multiple sql queries which should be done using addBatch
& executeBatch
.<br>
you don't have to execute use kstds
because the connection to the database is set via Java
try this:
String Query1 ="update match SET match.Team1Goals=match.Team1Goals+1 "
+ "where match.Team1ID= ( select team.TeamID from team where team.Name='AHLI' ) "
+ "and match.Matchid = 1 ; "
String Query2 ="Update match SET match.Team2Goals=match.Team2Goals+1 "
+ "where match.Team2ID= ( select team.TeamID from team where team.Name='AHLI' ) "
+ "and match.Matchid=1;";
//stmt is your Statement and conn is your Connection
con.setAutoCommit(false);
stmt.addBatch(Query1);
stmt.addBatch(Query2);
stmt.executeBatch();
con.commit();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论