造成原因java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 0).

来源:互联网 发布:社交软件使用调查 编辑:程序博客网 时间:2024/06/05 07:53

今天写代码遇到了

java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 0).
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2796)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2827)
at com.mysql.jdbc.PreparedStatement.setInt(PreparedStatement.java:2778)
at com.li.dao.StuDao.update(StuDao.java:53)
at com.li.dao.StuDao.main(StuDao.java:98)


是SQL语句错了,找了半天没有头绪,后来发现原来是

String sql = "update stu set username='?',password='?' where userid='?'";

改成  String sql = "update stu set username=?,password=? where userid=?"; 把?前后的''去掉就可以了。

我想好多人遇到这样的错误,都是这个原因吧。