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

huangapple go评论65阅读模式
英文:

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

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

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;";

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

答案1

得分: 1

你正在尝试执行多个 SQL 查询,应该使用 addBatchexecuteBatch 进行操作。
你不需要执行 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 =&quot;update match SET match.Team1Goals=match.Team1Goals+1 &quot;
			+ &quot;where match.Team1ID= ( select team.TeamID from team  where team.Name=&#39;AHLI&#39; ) &quot;
			+ &quot;and match.Matchid = 1 ; &quot;
String Query2 =&quot;Update match SET match.Team2Goals=match.Team2Goals+1 &quot;
			+ &quot;where match.Team2ID= ( select team.TeamID from team where team.Name=&#39;AHLI&#39; ) &quot;
			+ &quot;and match.Matchid=1;&quot;;
//stmt is your Statement and conn is your Connection  
con.setAutoCommit(false);	     
stmt.addBatch(Query1); 
stmt.addBatch(Query2);
stmt.executeBatch();
con.commit();

huangapple
  • 本文由 发表于 2020年5月4日 15:01:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/61586765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定