Spring JdbcTemplate Insert抛出未分类的SQLException

我正在使用Spring JDBCTemplate将新行插入到表中。 jdbcTemplate.update()抛出以下exception:

PreparedStatementCallback; 未分类的SQL []的SQLException; SQL状态[99999]; 错误代码[17090]; 操作不允许

但是无论如何都要将行插入到db中。

这是代码:

final StringBuilder widgetInsert = new StringBuilder(); widgetInsert.append("INSERT INTO WIDGET (ID, KEY, DEPT_NUM, TYPE, CREATED_BY_ID, CREATED_DATE) "); widgetInsert.append("VALUES (WIDGET_SEQ.NEXTVAL, ?, ?, ?, ?, ?) "); Long widgetId; try { KeyHolder kh = new GeneratedKeyHolder(); jdbcTemplate.update( new PreparedStatementCreator() { @Override public PreparedStatement createPreparedStatement(Connection con) throws SQLException { PreparedStatement ps = con.prepareStatement(widgetInsert.toString()); ps.setLong(1, key); ps.setLong(2, deptNumber); ps.setString(3, widgetType); ps.setLong(4, creatorId); ps.setLong(5, System.currentTimeMillis()); return ps; } }, kh); widgetId = kh.getKey().longValue(); } catch (Exception e) { // THIS EXCEPTION IS THROWN throw new WidgetException("Problem inserting widget for key=" + key, e); } 

编辑:运行Spring JDBC 3.2.4.RELEASE

完全例外:

[21] 15/02/02 15:04:59.537在StandardWrapperValve.invoke()中:[127] org.springframework.jdbc.UncategorizedSQLException:PreparedStatementCallback; 未分类的SQL []的SQLException; SQL状态[99999]; 错误代码[17090]; 不准操作; 嵌套exception是java.sql.SQLException:org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)在org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)中不允许进行操作org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)atg.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:605)org.springframework.jdbc.core.JdbcTemplate.update (jdbcTemplate.java:849)atg.acme.widget.service.WidgetServiceImpl.createWidget(WidgetServiceImpl.java:68)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java) :57)在org.spring的java.lang.reflect.Method.invoke(Method.java:606)的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) framework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation。 java:150)org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:96)位于org.springframework.transaction的org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)。位于org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)的interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)在com.sun.proxy。$ Proxy21.getOrCreatePersonalOwner(未知来源)org.acme.service.BigServiceImpl.addStuff(BigServiceImpl。 java:574)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang位于org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint的org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)中的.reflect.Method.invoke(Method.java:606)(ReflectiveMethodInvocation.java:183 )org.springframework.aop.frame.RevlectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:96)at org.springframework.transaction.interceptor.TransactionAspectSupport org.springframew上的org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)中的.invokeWithinTransaction(TransactionAspectSupport.java:260) ork.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)位于com.sun.proxy的org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)。$ Proxy22.addStuff(未知来源) at org.acme.endpoint.BigRs.addStuff(BigRs.java:262)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl .invoke(DelegatingMethodAccessorImpl.java:43)位于org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)的java.lang.reflect.Method.invoke(Method.java:606) Org.springframework上的org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)。 web.servlet.D org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)中的ispatcherServlet.doDispatch(DispatcherServlet.java:925)位于org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936)org位于org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java)的javax.servlet.http.HttpServlet.service(HttpServlet.java:646)中的.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838) :812)在org.apache.atache.atvice.httpServ.device(HttpServlet.java:727)的org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)org.apache.catalina.core.ApplicationFilterChain。在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)的orF.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)的orFilter(ApplicationFilterChain.java:208) .apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:2 08)org.springframework上的org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330)org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)位于org.acme.stack.auth.spring的org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)的.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) .AuthRequestParameterCleanupFilter.doFilter(AuthRequestParameterCleanupFilter.java:45)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java) :113)org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)at org.acme.stack.auth.spring.AuthContextSpringFilter.doFilter(AuthContextSpringFilter。 java:90)在org.springframework的org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) org.springframework.security.web.FilterChainProxy上的org.springframework.security.web.authentication.preauth.Abs​​tractPreAuthenticatedProcessingFilter.doFilter(AbstractPreAuthenticatedProcessingFilter.java:94)中的.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)位于org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy)的org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)上的$ VirtualFilterChain.doFilter(FilterChainProxy.java:342) .java:342)org.springframework.s的org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 在org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy。)的org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)的ecurity.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)。 java:160)org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343)org.apringframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:260)org.apache.catalina.core .applicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)org.acme.stack.web.servlet.filter.SessionlessFilter.doFilter(SessionlessFilter.java) :30)org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)org.springframework.web.filter。字符编码 在org.apringframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)的org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)的org.springterInternal(CharacterEncodingFilter.java:88)中的Filter.doFilterInternal(CharacterEncodingFilter.java:88) .apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:203)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve) .java:122)org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)位于org.apache.catalina的org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)。 valve.AccessLogValve.invoke(AccessLogValve.java:950)atg.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:683)org.acme.stack.tomcat.auth.AuthHostValve.invoke(AuthHostValve.java: 145)在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)在org 位于org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol)的org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)中的.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) .java:611)在java.util的java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)的org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:314)。 concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)atg.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Thread.java: 745)引起:java.sql.SQLException:oracle.jdbc.driver.OracleStatement.getGeneratedKeys(OracleStatement.java:5219)在oracle.jdbc.driver.OracleStatementWrapper.getGeneratedKeys(OracleStatementWrapper.java:366)的太阳下不允许进行操作sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodA)中的.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ccessorImpl.java:57)atg.apache.tomcat.jdbc.pool.interceptor上的java.lang.reflect.Method.invoke(Method.java:606)中的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) .sbstractQueryReport $ StatementProxy.invoke(AbstractQueryReport.java:235)at com.sun.proxy。$ Proxy89.getGeneratedKeys(Unknown Source)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl .java:57)在org.apache.tomcat.jdbc.pool.interceptor的java.lang.reflect.Method.invoke(Method.java:606)的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)。在sun.sref.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl。 java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(委托 MethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:606)at com.apache.tomcat.jdbc.pool.interceptor.StatementDecoratorInterceptor $ StatementProxy.invoke(StatementDecoratorInterceptor.java:252)at com org.springframework.jdbc.core.JdbcTemplate $ 3.doInPreparedStatement(JdbcTemplate.java:854)org.springframework.jdbc.core.JdbcTemplate $ 3.doInPreparedStatement(JdbcTemplate.java)中的.sun.proxy。$ Proxy89.getGeneratedKeys(未知来源) :849)org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:589)… 96更多

在我的代码中使用“org.springframework.jdbc.object.BatchSqlUpdate”时遇到了同样的问题:

  BatchSqlUpdate batchSqlUpdate = new BatchSqlUpdate(dataSource, getSqlString(), getSqlTypes(), 1); KeyHolder keyHolder = new GeneratedKeyHolder(); long result; int row = batchSqlUpdate.update(getObjParameters(tmpObj), keyHolder); if (row > 0) result = keyHolder.getKey().longValue(); 

它导致了这个错误:

SQL状态[99999]; 错误代码[17090]; 不准操作; 嵌套exception是java.sql.SQLException:不允许操作

在第一行之后添加这些行之后问题就解决了:

  batchSqlUpdate.setGeneratedKeysColumnNames(new String[]{"id"}); batchSqlUpdate.setReturnGeneratedKeys(true); 

好的,这对我来说已经解决了。 这可能是Oracle的要求 – 我不知道这是肯定的,因为我无法在H2上运行chenrui的测试代码。 显然,PreparedStatement要求显式声明已排序的列。 因此,对于我在序列中生成的主键列,我声明如下:

  final String generatedColumns[] = { "ID" }; 

然后我将数组传递给PreparedStatementCreator匿名内部类的createPreparedStatement()方法内的prepareStatement()调用:

  PreparedStatement ps = con.prepareStatement(widgetInsert.toString(), generatedColumns);