首页 > 解决方案 > 有例外!您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册以获取正确的语法

问题描述

我收到此错误我正在使用 java 和 javafx,它已连接到 MYsql DB 我在执行此语句从 java 到我的 sql 时收到此错误请帮助

有例外!

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

在此处输入图像描述

标签: javamysql

解决方案


您正在尝试执行多个应该使用 addBatch&完成的 sql 查询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 is your Statement and conn is your Connection  
con.setAutoCommit(false);        
stmt.addBatch(Query1); 
stmt.addBatch(Query2);
stmt.executeBatch();
con.commit();

推荐阅读