Parameter index of 21 is out of range (1, 18); nested exception is java.sql.SQLException: Parameter index of 21 is out of range (1, 18)
时间: 2023-11-07 08:52:15 浏览: 32
报错信息"Parameter index of 21 is out of range (1, 18); nested exception is java.sql.SQLException: Parameter index of 21 is out of range (1, 18)"意味着你在设置参数时使用了21作为索引,但是只有18个参数可用。这可能是因为你的SQL语句中只有18个问号(参数占位符),但你却尝试给第21个参数赋值。这会导致参数越界异常。
通常,这种错误发生的原因是在设置参数时没有正确地匹配问号的数量和要插入的值的数量。你需要检查你的SQL语句中的问号数量,并确保你提供的参数与问号一一对应。
相关问题
Exception in thread "main" org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL [updata account set username=?,balance=? where id=]; Parameter index out of range (3 > number of parameters, which is 2).; nested exception is java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
这个错误是由于你的 SQL 语句中的参数数量与传递给 PrepareStatement 的参数数量不一致导致的。具体来说,错误信息指出你的 SQL 语句中只有两个参数,但是你在设置参数时却传递了三个参数,因此出现了参数索引超出范围的错误。
解决这个问题的方法是检查你的 SQL 语句中的参数数量是否正确,并确保你在设置参数时传递的参数数量与 SQL 语句中的参数数量一致。如果你仍然无法解决这个问题,可能需要检查你的数据库连接和表结构是否正确。
Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property='userName', mode=IN, javaType=class java.lang.String, jdbcType=null, numericScale=null, resultMapId='null', jdbcTypeName='null', expression='null'}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).解决办法
这个错误提示中提到了两个问题,分别是JdbcType为null和参数索引超出范围。可以尝试以下解决方案:
1. 设置JdbcType:根据错误提示,可以尝试设置不同的JdbcType,以解决JdbcType为null的问题。例如,如果参数是一个字符串类型的参数,可以设置JdbcType为VARCHAR。
2. 检查参数索引:错误提示中还提到了参数索引超出范围的问题,这可能是由于在SQL语句中指定了参数,但实际传递的参数数量与SQL语句中的参数数量不匹配导致的。所以,需要仔细检查SQL语句和传递的参数,确保它们的数量和顺序是一致的。
如果上述方法都无法解决问题,还可以检查一下MyBatis和数据库连接的配置是否正确,以及是否有其他错误导致了这个异常。如果还是无法解决,可以尝试将错误信息和相关代码提交到相关的论坛或社区,寻求帮助。