日期:2014-05-18  浏览次数:20931 次

PreparedStatement 出错
String sql="insert into mytest values(null,?,?)";
PreparedStatement sta=db.con.prepareStatement(sql);
sta.setString(1,"11");
sta.setString(2, "");
System.out.print(sta);
sta.executeUpdate(sql);
报错
com.mysql.jdbc.JDBC4PreparedStatement@dd87b2: insert into mytest values(null,'11','22')
Exception in thread "main" 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 '?,?)' at line 1
我把insert into mytest values(null,'11','22')直接拿去运行没有问题,这是怎么回事啊?谢谢了

------解决方案--------------------
String sql="insert into mytest values(null,?,?)"; 
PreparedStatement sta=db.con.prepareStatement(sql); 
sta.setString(1,"11"); 
sta.setString(2, ""); 
System.out.print(sta); 
sta.executeUpdate(sql); //这里改成sta.executeUpdate();