2015-12-28 104 views
0
public Integer insertEncashLeaveRecordForEmp(
      final EncashedLeaveRecord encashRecord) { 
     KeyHolder keyHolder = new GeneratedKeyHolder(); 

     Integer update = null; 
     final String sql = "INSERT INTO lms_dat_encashcarryoverleaves(EncashCarry_NumOfLeaves," 
       + "fk_EncashCarry_empId,EncashCarry_AppliedDate, " 
       + "fk_EncashCarry_StatusId, fk_EncashCarry_LeaveTypeID, " 
       + "fk_EncashCarry_UpdatedById,EncashCarry_UpdatedOn) " 
       + " values (?,?,?,?,?,?,now())"; 
/* 
     try { 
      update = misJdbcTemplate 
        .update(sql, encashRecord.getNumberOfLeaves(), encashRecord 
          .getEmployeeId(), 
          new Date(new java.util.Date().getTime()), 
          encashRecord.getLeaveStatusId(), encashRecord 
            .getLeaveTypeId(), encashRecord 
            .getEmployeeId()); 
     } catch (Exception e) { 
      return 0; 
     }*/ 

     try { 

      jdbcTemplate.update(new PreparedStatementCreator() { 
       public PreparedStatement createPreparedStatement(
         Connection connection) throws SQLException { 
        PreparedStatement ps = connection.prepareStatement(sql, 
          new String[] { "pk_EncashCarry_ECId" }); 


        ps.setFloat(0, encashRecord.getNumberOfLeaves()); 
        ps.setInt(2, encashRecord.getEmployeeId()); 
        ps.setDate(3, new java.sql.Date(System.currentTimeMillis())); 
        ps.setInt(4,encashRecord.getLeaveStatusId()); 
        ps.setInt(5,encashRecord.getLeaveTypeId()); 
        ps.setInt(6,encashRecord 
          .getEmployeeId()); 


        return ps; 
       } 
      }, keyHolder); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 


     return keyHolder.getKey().intValue(); 
    } 

我想使用spring jdbc模板保存上面的值,但得到這個異常。參數索引超出範圍(0 <1)進入春季jdbctemplate

異常

org.springframework.dao.TransientDataAccessResourceException: PreparedStatementCallback; SQL []; Parameter index out of range (0 < 1).; nested exception is java.sql.SQLException: Parameter index out of range (0 < 1). 
    at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:107) 
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) 
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80) 
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602) 
    at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:842) 
    at com.thbs.intranet.rest.dao.misdatabase.impl.EncashedLeaveRecordsDaoImpl.insertEncashLeaveRecordForEmp(EncashedLeaveRecordsDaoImpl.java:244) 
    at com.thbs.intranet.rest.service.impl.EmployeeLeaveServiceImpl.applyEncashLeaveByEmp(EmployeeLeaveServiceImpl.java:1218) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
    at com.sun.proxy.$Proxy33.applyEncashLeaveByEmp(Unknown Source) 
    at com.thbs.intranet.rest.resource.EmployeeResource.applyEncashLeaveByEmp(EmployeeResource.java:934) 
    at com.thbs.intranet.rest.resource.EmployeeResource$$FastClassByCGLIB$$f9a9f6b5.invoke(<generated>) 
    at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) 
    at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:688) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
    at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:55) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:50) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:621) 
    at com.thbs.intranet.rest.resource.EmployeeResource$$EnhancerByCGLIB$$1d5273d3.applyEncashLeaveByEmp(<generated>) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:187) 
    at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:70) 
    at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:279) 
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:136) 
    at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:86) 
    at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:136) 
    at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:74) 
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1357) 
    at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1289) 
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1239) 
    at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1229) 
    at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:420) 
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:497) 
    at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:684) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:368) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) 
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) 
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) 
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) 
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) 
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) 
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) 
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380) 
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169) 
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237) 
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
    at java.lang.Thread.run(Unknown Source) 
Caused by: java.sql.SQLException: Parameter index out of range (0 < 1). 

回答

2

在用預處理的索引參數開始於1,不是0。如果檢查Oracle文檔:

https://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html

在任何方法中描述:

setFloat(int parameterIndex, double x)

個參數: parameterIndex - 第一個參數是1,第二個是2,... x - 參數值

所以嘗試改變:

ps.setFloat(0, encashRecord.getNumberOfLeaves()); 

ps.setFloat(1, encashRecord.getNumberOfLeaves()); 
相關問題