当前位置: 代码迷 >> Java Web开发 >> PreparedStatement 出错解决方法
  详细解决方案

PreparedStatement 出错解决方法

热度:748   发布时间:2016-04-17 12:35:59.0
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();
  相关解决方案