我在Spring JDBCTemplate的表中插入一个新行。 jdbcTemplate.update()抛出以下异常:
PreparedStatementCallback;未分类的SQL []的SQLException; SQL状态[99999];错误代码[17090];不允许操作
但是无论如何都要将行插入数据库。
以下是代码:
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];不准操作;嵌套异常是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) 在org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:605) 在org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:849) 在org.acme.widget.service.WidgetServiceImpl.createWidget(WidgetServiceImpl.java:68) 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.reflect.Method.invoke(Method.java:606) 在org.springframework.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) at org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:96) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) 在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) at com.sun.proxy。$ Proxy21.getOrCreatePersonalOwner(Unknown Source) 在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.reflect.Method.invoke(Method.java:606) 在org.springframework.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) at org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:96) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260) 在org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94) 在org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 在org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204) 在com.sun.proxy。$ Proxy22.addStuff(未知来源) 在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) at java.lang.reflect.Method.invoke(Method.java:606) 在org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) 在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440) 在org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428) 在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) 在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) 在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) 在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:838) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:646) 在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:330) 在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) 在org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.acme.stack.auth.spring.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) 在org.acme.stack.auth.spring.AuthContextSpringFilter.doFilter(AuthContextSpringFilter.java:90) 在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 $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter.doFilter(AbstractPreAuthenticatedProcessingFilter.java:94) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 在org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 在org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:343) 在org.springframework.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.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) 在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:203) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) 在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) 在org.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.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421) 在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070) at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:611) at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:314) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(Thread.java:745) 引起:java.sql.SQLException:不允许操作 at oracle.jdbc.driver.OracleStatement.getGeneratedKeys(OracleStatement.java:5219) at oracle.jdbc.driver.OracleStatementWrapper.getGeneratedKeys(OracleStatementWrapper.java:366) 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.reflect.Method.invoke(Method.java:606) at org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport $ 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.apache.tomcat.jdbc.pool.interceptor.AbstractQueryReport $ 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.apache.tomcat.jdbc.pool.interceptor.StatementDecoratorInterceptor $ StatementProxy.invoke(StatementDecoratorInterceptor.java:252) at com.sun.proxy。$ Proxy89.getGeneratedKeys(Unknown Source) 在org.springframework.jdbc.core.JdbcTemplate $ 3.doInPreparedStatement(JdbcTemplate.java:854) 在org.springframework.jdbc.core.JdbcTemplate $ 3.doInPreparedStatement(JdbcTemplate.java:849) 在org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:589) ......还有96个
答案 0 :(得分:3)
使用“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];不准操作;嵌套异常是java.sql.SQLException:不允许操作
在第一行之后添加这些行之后,问题就解决了:
batchSqlUpdate.setGeneratedKeysColumnNames(new String[]{"id"});
batchSqlUpdate.setReturnGeneratedKeys(true);
答案 1 :(得分:1)
好的,这对我来说已经解决了。这可能是Oracle的要求 - 我不确定,因为我无法在H2上运行chenrui的测试代码。显然,PreparedStatement要求显式声明已排序的列。因此,对于我在序列中生成的主键列,我声明如下:
final String generatedColumns[] = { "ID" };
然后我将数组传递到PreparedStatementCreator匿名内部类的createPreparedStatement()方法内的prepareStatement()调用中:
PreparedStatement ps = con.prepareStatement(widgetInsert.toString(), generatedColumns);