将表单的值循环插入到数据库中,报错,求大神帮忙!

时间:2021-09-07 21:44:46
源代码:
try{ 
if(boo2){
for(int i=1;i<=optionNum;i++){
con=DriverManager.getConnection(url);
String optionID =request.getParameter("optionID_"+i);
String optionContent=handleString(request.getParameter("optionContent_"+i));
String isBlankFilling=handleString(request.getParameter("isBlankFilling_"+i));
String sql2="insert into option (optionID,questionID,optionContent,isBlankFilling,number) values('"+optionID+"','"+queID+"','"+optionContent+"','"+isBlankFilling+"','"+optionNum+"')";
Statement stmt=con.createStatement();
int m =stmt.executeUpdate(sql2);
if(m!=0){

option.setOptionID(optionID);
option.setOptionContent(optionContent);
option.setBlankFilling(isBlankFilling);
}
    else{
     backNews="未填写问题编号";
    question.setBackNews(backNews);
    }
con.close();
}

}

}
catch(SQLException exp){

backNews="增加失败,请重新添加"+exp;
question.setBackNews(backNews);
}
代码报错:
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 'option (optionID,questionID,optionContent,isBlankFilling,number) values('A','1',' at line 1
在网上查了一些,说是因为statement如果没关闭,insert语句不能循环添加,所以我就把con放在循环里了,但还是报错。。求大神帮忙啊!!!跪谢!

2 个解决方案

#1


con.close();
后面将 stmt  也关闭
加上 stmt.close();

#2


谢谢,解决了~另外还改了点sql语句

#1


con.close();
后面将 stmt  也关闭
加上 stmt.close();

#2


谢谢,解决了~另外还改了点sql语句