日期:2014-05-20  浏览次数:20643 次

Java对数据库的操作!!
String ques="ques";
        String quesNum;
        String insertQues;
        for(int m=1;m<=2;m++){
         int m=1;
         quesNum=ques+m;
         insertQues="update question.testPage set '"+quesNum+"'='"+result[m]+"' where userID='"+username+"'";
         stmt.execute(insertQues);
         ques="";
        }


提示的错误为: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 ''ques1'='8' where userID='123456'' at line 1
result[]是一个int 的数组。

------解决方案--------------------
引用:
额。。。。那请问我应该怎么写好一些呢?我就想想数据库里的动态的插入一些数据

楼主使用的是Statement,推荐使用PreparedStatement
即可防止注入SQL注入漏洞,效率一般也高(预编译)
而且可以使用占位符 "?" 实例用法:
String sql="select * from test where usernmae=? and password=?";
PreparedStatement psm=conn.preparedStatement(sql);
psm.setString(1,myname);
psm.setString(2,mypasswd);
Result rs=psm.executeQuery();
if(rs.next){
rs.close();
con.close();
return false;
}
else{
rs.close();
con.close();
return true;
}


------解决方案--------------------
''ques1'='8' where userID='123456''应该是这个错误,set 后面的列名称不需要加单引号,列名称后面的参数是需要加单引号!
至于写法么,你可以用PreparedStatement,这样只需要sql编译一次,然后参数变化就可以。