调用MYSQL存储过程引发的“Parameter index of 5 is out of range (1, 4)”问题
错误信息如下:
0:13:58,218 INFO SbxxServiceImpl:19 - 开始执行SbxxServiceImpl类的 createSblx 方法 !
10:13:58,218 INFO SbxxDaoImpl:24 - 开始执行SbxxDaoImpl类的 createSblx 方法 !
10:13:58,218 INFO SbxxDaoImpl:32 - 执行新增设备类型操作 方法 ,调用存储过程 : call ps_ddgl_sblx_insert(yyyy ,yyy ,yyyyy,3,0) ;
10:13:58,218 ERROR SbxxDaoImpl:47 - 执行新增设备类型操作出错了,导致原因可能是:< Parameter index of 5 is out of range (1, 4)>
10:13:58,218 ERROR SbxxDaoImpl:50 - com.mysql.jdbc.CallableStatement.checkParameterIndexBounds(CallableStatement.java:1002)
10:13:58,218 ERROR SbxxDaoImpl:50 - com.mysql.jdbc.CallableStatement.checkIsOutputParam(CallableStatement.java:971)
10:13:58,218 ERROR SbxxDaoImpl:50 - com.mysql.jdbc.CallableStatement.registerOutParameter(CallableStatement.java:864)
10:13:58,218 ERROR SbxxDaoImpl:50 - org.apache.commons.dbcp.DelegatingCallableStatement.registerOutParameter(DelegatingCallableStatement.java:95)
10:13:58,218 ERROR SbxxDaoImpl:50 - org.apache.commons.dbcp.DelegatingCallableStatement.registerOutParameter(DelegatingCallableStatement.java:95)
10:13:58,218 ERROR SbxxDaoImpl:50 - com.zichen.zhhwd.sbxx.dao.impl.SbxxDaoImpl.createSblx(SbxxDaoImpl.java:39)
10:13:58,218 ERROR SbxxDaoImpl:50 - com.zichen.zhhwd.sbxx.service.impl.SbxxServiceImpl.createSblx(SbxxServiceImpl.java:20)
10:13:58,218 ERROR SbxxDaoImpl:50 - com.zichen.zhhwd.action.sbxx.SblxAction.doaddsblx(SblxAction.java:68)
10:13:58,218 ERROR SbxxDaoImpl:50 - sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
10:13:58,234 ERROR SbxxDaoImpl:50 - sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
10:13:58,234 ERROR SbxxDaoImpl:50 - sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
10:13:58,234 ERROR SbxxDaoImpl:50 - java.lang.reflect.Method.invoke(Method.java:597)
CREATE
PROCEDURE ps_ddgl_sblx_insert(IN ps_lxmc VARCHAR(20) CHARACTER SET UTF8, IN ps_lxjx VARCHAR(3) CHARACTER SET UTF8, IN ps_lxbz VARCHAR(50) CHARACTER SET UTF8, IN ps_ggwsx int(11),OUT exceResult INT)
COMMENT '新增设备类型'
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
SET exceResult =0;
START TRANSACTION;
select count(*) into @likeset from ddgl_sblx where lxmc = ps_lxmc or lxjx= ps_lxjx ;
if (@likeset >0) then
SET exceResult = 2;
else
INSERT INTO ddgl_sblx ( lxmc, lxjx, lxbz,ggwsx) VALUES (ps_lxmc, ps_lxjx, ps_lxbz,ps_ggwsx);
COMMIT;
SET exceResult = 1;
end if ;
END
Logger log = Logger.getLogger(SbxxDaoImpl.class);
public int createSblx(Sblx item) {
log.info("开始执行SbxxDaoImpl类的 createSblx 方法 !");
Connection con = null;
CallableStatement ps = null;
int insertResult = 0;
try {
con = JdbcUtil.getInstance().getConnection();
con.setAutoCommit(false);
ps = con.prepareCall(" call ps_ddgl_sblx_insert(?,?,?,?,?) ; ");
log.info(" 执行新增设备类型操作 方法 ,调用存储过程 : call ps_ddgl_sblx_insert("
+ item.getLxmc() + " ," + item.getLxjx() + " ,"
+ item.getLxbz() +","+item.getGgwsx()+",0) ;");
ps.setString(1, item.getLxmc());
ps.setString(2, item.getLxjx());
ps.setString(3, item.getLxbz());
ps.setInt(4, item.getGgwsx());
ps.registerOutParameter(5, Types.INTEGER);
ps.execute();
insertResult = ps.getInt(5);
if (insertResult == 1) {
log.info("执行新增设备类型操作 操作数据库成功!");
return insertResult;
}
} catch (SQLException e) {
log.error(" 执行新增设备类型操作出错了,导致原因可能是:< " + e.getMessage() + "> ");
StackTraceElement[] error = e.getStackTrace();
for (StackTraceElement stackTraceElement : error) {
log.error(stackTraceElement.toString());
}
} finally {
JdbcUtil.close(null, ps, con);
}
return insertResult;
}
[解决办法]
你procedure4个输入参数
[解决办法]