首页 文章

未分类的SQLException Jdbc模板

提问于
浏览
2

我正在尝试调用存储过程来在SQL表中创建项目 . 运行代码时,将运行存储过程并创建该项,但会引发此错误 . 存储在表中的值是正确的,生成密钥生成正确的密钥 .

这是我的创建函数,我在其中创建参数映射:

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调用SEVERE:servlet [dispatcher]的Servlet.service()在上下文中,路径[]引发异常[请求处理失败;嵌套异常是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)at 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.executeStatement(SQLServerStatement.java:155)中的microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)位于com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement . java:314)org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:98)at org.apache.commons.dbcp2.DelegatingPreparedStatement.executeUpdate(Delegat) ingPreparedStatement.java:98)org.springframework.jdbc.core.JdbcTemplate $ 3.doInPreparedStatement(JdbcTemplate.java:946)org.springframework.jdbc.core.JdbcTemplate $ 3.doInPreparedStatement(JdbcTemplate.java:943)org.springframework位于org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:943)的.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:644)org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.update(NamedParameterJdbcTemplate .java:344)位于com.crown的com.crown.datapoint.services.JobService.create(JobService.java:91)的com.crown.datapoint.services.dao.JobDAO.create(JobDAO.java:94) . 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 . 在java.lang.reflect.Method.invoke(Method.java:606)上调用(DelegatingMethodAccessorImpl.java:43)at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)位于org.springframework.web.servlet的org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137) . mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)位于org.springframework.web.servlet的org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777) . mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)位于org.springframework.web.servlet.DispatcherServlet的org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) . 位于org.springframework.web.servlet.FrameworkServle的org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)的doDispatch(DispatcherServlet.java:943)位于org.springframework的javax.servlet.http.HttpServlet.service(HttpServlet.java:650)org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)的t.processRequest(FrameworkServlet.java:966) .web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)在javax.servlet.http.HttpServlet.service(HttpServlet.java:731)atorg.apache.catalina.core.ApplicationFilterChain.internalDoFilter在org.apache.tomcat.websocket.server.WsFilter(ApplicationFilterChain.java:303)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) . 的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(E xceptionTranslationFilter.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的org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)中的.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)位于org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java)的org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:154)上的$ VirtualFilterChain.doFilter(FilterChainProxy.java:342) :342)org.springframework.securit上的org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) y.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)在org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:125)在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain . 在org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)的org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:199)中的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.we b.FilterChainProxy $ VirtualFilterChain.doFilter(FilterChainProxy.java:342)org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)at org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter( FilterChainProxy.java:342)org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)at 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.DelegatingFilt erProxy.doFilter(DelegatingFilterProxy.java:261)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)在组织.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 . 进程(AbstractHttp11Processor.java:1079)org.apache.coyote.AbstractProtocol $ AbstractC onnectionHandler.process(AbstractProtocol.java:620)atg.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:316)at 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)at java.lang.Thread.run (Thread.java:745)

编辑:

我修改了我的代码(见下文)并且它再次工作...我将0返回到restController,它最初将id显示为0 ...但是在页面刷新时解析为正确的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;

}

2 回答

  • 0

    您是否可以在可以启用跟踪的服务器上询问服务器端跟踪或重现它?因为客户端上的未知错误可以在SQL Server服务器上以更具体的方式记录 .

  • 0

    当我调用其他存储过程时遇到了同样的问题 . 我将调用存储过程的方法更改为以下内容,似乎可以正常工作 . 我不完全确定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");
    }
    

相关问题