我尝试调用存储过程来在SQL表中创建项目。运行代码时,将运行存储过程并创建该项,但会引发此错误。存储在表中的值是正确的,生成密钥生成正确的密钥。
这是我的创建函数,我在其中创建参数map:
public int create(Job job) throws DataAccessException, NullPointerException {
Assert.notNull(job,"Job must not be null.");
Assert.notNull(job.getSite(),"Site must not be null.");
KeyHolder keyHolder = new GeneratedKeyHolder();
final String columnNames[] = {"id"};
MapSqlParameterSource params = params("intSiteId",job.getSite().getId())
.addValue("dtmStart", job.getStartDate() == null ? null : dateTimeString(job.getStartDate()))
.addValue("dtmEnd", job.getEndDate() == null ? null : dateTimeString(job.getEndDate()))
.addValue("intManualFl",job.isManual());
getTemplate().update(getSql("create"), params, keyHolder, columnNames);
return keyHolder.getKey().intValue();
}
更新NamedParameterJdbcTemplate类中的函数
@Override
public int update(
String sql, SqlParameterSource paramSource, KeyHolder generatedKeyHolder, String[] keyColumnNames)
throws DataAccessException {
ParsedSql parsedSql = getParsedSql(sql);
String sqlToUse = NamedParameterUtils.substituteNamedParameters(parsedSql, paramSource);
Object[] params = NamedParameterUtils.buildValueArray(parsedSql, paramSource, null);
List<SqlParameter> declaredParameters = NamedParameterUtils.buildSqlParameterList(parsedSql, paramSource);
PreparedStatementCreatorFactory pscf = new PreparedStatementCreatorFactory(sqlToUse, declaredParameters);
if (keyColumnNames != null) {
pscf.setGeneratedKeysColumnNames(keyColumnNames);
}
else {
pscf.setReturnGeneratedKeys(true);
}
return getJdbcOperations().update(pscf.newPreparedStatementCreator(params), generatedKeyHolder);
}
例外:
2015年5月28日上午11:17:02 org.apache.catalina.core.StandardWrapperValve调用 严重:Servlet [dispatcher]的Servlet.service()与path []的上下文引发了异常[请求处理失败;嵌套异常是org.springframework.jdbc.UncategorizedSQLException:PreparedStatementCallback;未分类SQL的SQLException [{call usp_createNewTrans(?,?,?,?)}]; SQL状态[null];错误代码[0];生成结果集以进行更新。嵌套异常是com.microsoft.sqlserver.jdbc.SQLServerException:为更新生成了一个结果集。 com.microsoft.sqlserver.jdbc.SQLServerException:生成用于更新的结果集。 at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190) 在com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:417) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement $ PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350) 在com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696) 在com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715) 在com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180) 在com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155) 在com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:314) 在org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98) 在org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98) 在org.springframework.jdbc.core.JdbcTemplate $ 3.doInPreparedStatement(JdbcTemplate.java:946) 在org.springframework.jdbc.core.JdbcTemplate $ 3.doInPreparedStatement(JdbcTemplate.java:943) 在org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644) 在org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:943) 在org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate.java:344) 在com.crown.datapoint.services.dao.JobDAO.create(JobDAO.java:94) 在com.crown.datapoint.services.JobService.create(JobService.java:91) 在com.crown.datapoint.web.controller.JobRestController.createJob(JobRestController.java:63) 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.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221) 在org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) 在org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777) 在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706) 在org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 在org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) 在org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) 在org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) 在org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:650) 在org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:731) 在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.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110) 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342) 在org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:57) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在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.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50) 在org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 在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:344) 在org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261) 在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:220) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) 在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) 在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:957) 在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) 在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) 在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:620) at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:316) 在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)
我将我的代码修改为此(见下文)并且它再次起作用...我将0返回到最初将id显示为0的restController ...但是在页面刷新时解析为正确的id,但我觉得像这样并不完全是解决方案。
public int create(Job job) throws DataAccessException, NullPointerException {
Assert.notNull(job,"Job must not be null.");
Assert.notNull(job.getSite(),"Site must not be null.");
MapSqlParameterSource params = params("intSiteId",job.getSite().getId())
.addValue("dtmStart", job.getStartDate() == null ? null : dateTimeString(job.getStartDate()))
.addValue("dtmEnd", job.getEndDate() == null ? null : dateTimeString(job.getEndDate()))
.addValue("intManualFl",job.isManual());
getTemplate().update(getSql("create"), params);
return 0;
}
答案 0 :(得分:0)
您是否可以在可以启用跟踪的服务器上询问服务器端跟踪或重现它?因为客户端上的未知错误可以在SQL Server服务器上以更具体的方式记录。
答案 1 :(得分:-1)
当我调用其他存储过程时遇到了同样的问题。我将调用存储过程的方法更改为以下内容,似乎可以正常工作。我不完全确定doInPreparedStatement是如何工作的,所以可能有更好的方法来使用它,但这似乎解决了我的问题。
public int create(Job job) {
Assert.notNull(job,"Job must not be null.");
Assert.notNull(job.getSite(),"Site must not be null.");
MapSqlParameterSource params = params("intSiteId",job.getSite().getId())
.addValue("dtmStart", job.getStartDate() == null ? null : dateTimeString(job.getStartDate()))
.addValue("dtmEnd", job.getEndDate() == null ? null : dateTimeString(job.getEndDate()))
.addValue("intManualFl",job.isManual())
.addValue("insertDate",dateTimeString(DateTime.now(DateTimeZone.UTC)));
getTemplate().execute(getSql("create"), params, new PreparedStatementCallback<Boolean>(){
@Override
public Boolean doInPreparedStatement(PreparedStatement ps)
throws SQLException, DataAccessException {
return 0;
}
});
return (int)params.getValue("intSiteId");
}