考虑下面的代码,

String query1 = "insert into quizquestion (ques, quizId) values "
    + "('"+ques+"', '"+quizId+"')";
s = con.createStatement();
s.executeUpdate(query1, Statement.RETURN_GENERATED_KEYS);
rs = s.getGeneratedKeys();
if (rs.next()){
    quesId=rs.getString(1);
}

con.setAutoCommit(false);
String query2 = "insert into quizOption (option, quizQuesId, correct) values (?,?,?)";
ps = con.prepareStatement(query2);
for(int i=0; i<options.length; i++){
    ps.setString(1, options[i]);
    ps.setString(2, quesId);
    if(correctOption.equals((i+1)+"")){
        ps.setString(3, "1");
    }else{
        ps.setString(3, "0");
    }
    ps.addBatch();
}
int x[] = ps.executeBatch();
con.commit();

con.close();
return true;


问题是我的query1已成功执行,但是我收到了query2异常

我收到的一个示例错误如下,

java.sql.BatchUpdateException: 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 'option, quizQuesId, correct) values ('o13','16','1')' at line 1


有什么想法为什么我会得到这个例外?
提前致谢。

最佳答案

您的query2包含MySQL reserved keywordoption。这很可能导致问题。尝试将列名称括在引号(“)或反引号(`)中,如下所示:

String query2 = "insert into quizOption (\"option\", quizQuesId, correct) values (?,?,?)";

07-26 03:52