Consider the below code,
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;
The problem is my query1
is executed successfully, however I get an exception for query2
One sample error that I'm getting is as follows,
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
Any ideas why I'm getting this exception? Thanks in advance.