使用JDBC报错“Parameter index out of range (2 > number of parameters, which is 1).“
  TEZNKK3IfmPf 2023年11月12日 34 0

异常

在使用JDBC的PreparedStatement的时候报错:

Exception in thread "main" java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:959)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:862)
	at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3319)
	at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3304)
	at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3341)
	at com.mysql.jdbc.PreparedStatement.setLong(PreparedStatement.java:3358)
	at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:3529)
	at com.mysql.jdbc.JDBC42PreparedStatement.setObject(JDBC42PreparedStatement.java:68)
	at com.demo.bean.dao.BaseMapper.updateById(BaseMapper.java:289)
	at com.demo.bean.dao.Test.main(BaseMapper.java:143)

原因

根据堆栈跟踪信息提示:在SQL语句中找到了1个问号,即"which is 1"提示的信息;但却插入了2个值,即"2 > number of parameters"提示的信息,所以有问题。

也就是说发生错误的原因是当设置参数时,没有相应的问号与之匹配,然后导致参数越界。

使用JDBC报错“Parameter index out of range (2 > number of parameters, which is 1).“

 查看SQL语句,确实只有一个问号,所以要传入正确的SQL语句。

解决

传入正确的SQL语句,即添加对应参数个数的问号。

使用JDBC报错“Parameter index out of range (2 > number of parameters, which is 1).“

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月17日   48   0   0 dremiosqltable
  TEZNKK3IfmPf   2024年04月26日   56   0   0 java数据库sql
  TEZNKK3IfmPf   2024年05月17日   53   0   0 sqlmysql
  TEZNKK3IfmPf   2024年05月17日   38   0   0 sqlcube
  TEZNKK3IfmPf   2024年04月26日   30   0   0 javasql
  TEZNKK3IfmPf   2024年04月19日   56   0   0 sqlUser
TEZNKK3IfmPf