Java对数据库的操作!解决办法
Java对数据库的操作!!
提示的错误为: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注入漏洞,效率一般也高(预编译)
而且可以使用占位符 "?" 实例用法:
------解决方案--------------------
''ques1'='8' where userID='123456''应该是这个错误,set 后面的列名称不需要加单引号,列名称后面的参数是需要加单引号!
至于写法么,你可以用PreparedStatement,这样只需要sql编译一次,然后参数变化就可以。
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编译一次,然后参数变化就可以。