查数据库异常:java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

在执行数据库查询时遇到了`java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1)`的错误。问题源于MyBatis在设置参数时出现了错误,可能的原因是SQL查询中的占位符数量与实际传递的参数数量不匹配。解决方案是检查MyBatis的XML配置文件,确保所有的注释正确无误,特别是参数的使用。例如,将单行注释`--`更改为`<!--`和`-->`以避免解析错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java查询数据库数据报下面异常:
java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).

14-Feb-2022 15:38:05.578 严重 [http-nio-8080-exec-4] org.apache.catalina.core.StandardWrapperValve.invoke 在路径为/rbacDemo_war_exploded的上下文中,Servlet[springmvc]Servlet.service()引发了具有根本原因的异常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 #2 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 (2 > number of parameters, which is 1).
	java.sql.SQLException: Parameter index out of range (2 > number of parameters, which is 1).
		at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
		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:861)
		at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3382)
		at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3367)
		at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4083)
		at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.lang.reflect.Method.invoke(Method.java:498)
		at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:67)
		at com.sun.proxy.$Proxy32.setString(Unknown Source)

解决:查看mybatis的.xml文件中注释的地方,看一下注释的方式:
如果是类似下面注释:
源代码:

--         SELECT * from users u...
--         where u.username=#{username}

则应该改为:


<!--       SELECT * from users u...
        and u.username=#{username}-->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值