java MySQL SQL 语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30139093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:32:23  来源:igfitidea点击:

Error in your MySQL SQL syntax

javamysqlsqljdbcprepared-statement

提问by Ali Alhamaly

I'am trying to insert a record in my MySQL database, the record should be inserted in table medication_profile, but I get this error that says :

我正在尝试在我的 MySQL 数据库中插入一条记录,该记录应插入 table medication_profile,但我收到此错误消息:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.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 ' medication_type, diabetic_id, times_a_day, dose) VALUES ('injections',2,1,'18')' at line 1

线程“main”com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException 中的异常:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的“药物类型、糖尿病_id、时间_a_天、剂量)VALUES('injections',2,1,'18')'附近使用的正确语法

The code I'am using in java to insert this record is as follow:

我在java中用来插入这条记录的代码如下:

PreparedStatement addMedicationStm = con.prepareStatement("INSERT INTO medication_profile"
            + ", medication_type, diabetic_id, times_a_day, dose) "
            + "VALUES (?,?,?,?))", Statement.RETURN_GENERATED_KEYS);
    addMedicationStm.setString(1, medication.getMedicationType());
    addMedicationStm.setInt(2, medication.getDiabeticID());
    addMedicationStm.setInt(3, medication.getTimesPerDay());
    addMedicationStm.setString(4, medication.getDose());
    addMedicationStm.executeUpdate();

    ResultSet rs = addMedicationStm.getGeneratedKeys();

    if (rs != null && rs.next()) {
        medication.setDiabeticID(rs.getInt(1));
        addMedicationStm.close();
        con.close();
    }

I can't find where the error is, as I see the insert syntax is correct

我找不到错误在哪里,因为我看到插入语法是正确的

any suggestions?

有什么建议?

thanks in advance

提前致谢

回答by Christopher Schr?der

Your string query gets concatenated to:

您的字符串查询被连接到:

INSERT INTO medication_profile, medication_type, diabetic_id, times_a_day, dose) VALUES (?,?,?,?)

which should be something like

应该是这样的

INSERT INTO medication_profile (medication_type, diabetic_id, times_a_day, dose) VALUES (?,?,?,?)

回答by Junaid

remove the comma after table name and add (

删除表名后的逗号并添加 (

PreparedStatement addMedicationStm = con.prepareStatement("INSERT INTO medication_profile"
        + "(medication_type, diabetic_id, times_a_day, dose) "

回答by DoubleMa

Your sql syntax is wrong here:

你的 sql 语法在这里是错误的:

PreparedStatement addMedicationStm = con.prepareStatement("INSERT INTO medication_profile"
        + ", medication_type, diabetic_id, times_a_day, dose) "
        + "VALUES (?,?,?,?))", Statement.RETURN_GENERATED_KEYS);

the syntax should be something like :

语法应该是这样的:

 PreparedStatement addMedicationStm = con.prepareStatement("INSERT INTO "
       + "yourtableName "
       + "(column1Name, column2Name,...) " 
       + "VALUES (?,?,...)", Statement.RETURN_GENERATED_KEYS);

回答by Mureinik

You have misplaced your brackets and commas:

您放错了括号和逗号:

PreparedStatement addMedicationStm = 
    con.prepareStatement
        ("INSERT INTO medication_profile"
            + "(medication_type, diabetic_id, times_a_day, dose) "
            + "VALUES (?,?,?,?)";