Java.SQL 错误:Prepared Statement 不在批处理模式下

Posted

技术标签:

【中文标题】Java.SQL 错误:Prepared Statement 不在批处理模式下【英文标题】:Java.SQL Error: Prepared Statement not in batch mode 【发布时间】:2015-08-17 09:56:55 【问题描述】:

我正在使用 Java 和后端 HSQL。我准备好的声明早些时候工作正常。最近,我创建了部门和项目表之间的依赖关系,并将 dep_seq_id 列从自动生成的 seq_id 变成了一个简单的列(我们现在正在通过 java 代码更新 id)。在这些更改之后,所有依赖表的创建/更新都失败并出现错误,例如 “语句不在批处理模式下”。

表格结构如下:

CREATE TABLE DEPARTMENT
  (
    DEP_SEQ_ID  INTEGER NOT NULL,
    DEPT_STATE  VARCHAR(6) NOT NULL,
    NAME        VARCHAR(20) NOT NULL,
    YEAR        VARCHAR(4),
    CREATE_DATE DATE,
    LINK        VARCHAR(255),
    PRIMARY KEY(SEQ_ID,DEPT_STATE)
  );

CREATE TABLE ITEMS
  (
    ITEM_ID    INTEGER NOT NULL,
    ITEM_STATE VARCHAR(6) NOT NULL,
    NAME       VARCHAR(20),
    ITEM_CODE  VARCHAR(10),
    DEP_SEQ_ID INTEGER,
    DEPT_STATE VARCHAR(6),
    FOREIGN KEY(DEP_SEQ_ID,DEPT_STATE) REFERENCES DEPARTMENT(SEQ_ID, DEPT_STATE) ON
  DELETE CASCADE ON
  UPDATE CASCADE
  );

我不明白我在这里做错了什么。以下是我尝试在 Items 表中插入数据的代码,部分代码如下所示。我希望这是有道理的,我已经更改了名称和列,因为我无法分享实际代码。

 PreparedStatement statement = null;
    ResultSet generatedKeys = null;
    Long mappingId = null;

    try 
        statement = m_connection.prepareStatement("insert into ITEMS (ITEM_ID, ITEM_STATE, NAME, ITEM_CODE, DEP_SEQ_ID, DEPT_STATE) values(?,?,?,?,?,?)");
        for(WersMapping wersMapping : wersMapList)

        statement.setLong(1, item.getItemId());
        statement.setInt(2, item.getState());
        statement.setString(3, item.getName());
        statement.setString(4, item.getItemCode());

        statement.setInt(5, item.getDeptId());
        statement.setString(6, item.getDeptState());
        statement.addBatch();
        

    int[] nArray = statement.executeBatch();
    int totalInserts = 0;
    if(nArray != null)

        for(int n:nArray)
            totalInserts = totalInserts + n;
        
        LOGGER.log(Level.INFO, "Successfully created "+totalInserts + " Mapping instances");
    
    return new Long(totalInserts) ;

错误堆栈:

Aug 17, 2015 3:42:33 PM ItemMapDao create
SEVERE: Error inserting into Items Mapping table
java.sql.SQLException: statement is not in batch mode
        at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
        at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
        at org.hsqldb.jdbc.Util.sqlExceptionSQL(Unknown Source)
        at org.hsqldb.jdbc.JDBCPreparedStatement.executeBatch(Unknown Source)
        at com.ItemMapDao.create(ItemMapDao.java:367)
        at access.MaintanenceDac.updateFeatureSpecificData(MaintanenceDac.java:907)
        at access.MaintanenceDac.updateFeatureState(MaintanenceDac.java:649)
        at access.MaintanenceDac.updateCell(MaintanenceDac.java:154)
        at services.MaintainServices.updateSystemFeature(MaintainServices.java:156)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.wink.server.internal.handlers.InvokeMethodHandler.handleRequest(InvokeMethodHandler.java:63)
        at org.apache.wink.server.handlers.AbstractHandler.handleRequest(AbstractHandler.java:33)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)
        at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63)
        at org.apache.wink.server.internal.handlers.CreateInvocationParametersHandler.handleRequest(CreateInvocationParametersHandler.java:54)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)
        at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63)
        at org.apache.wink.server.internal.handlers.FindResourceMethodHandler.handleSubResourceMethod(FindResourceMethodHandler.java:183)
        at org.apache.wink.server.internal.handlers.FindResourceMethodHandler.handleRequest(FindResourceMethodHandler.java:110)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)
        at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63)
        at org.apache.wink.server.internal.handlers.FindRootResourceHandler.handleRequest(FindRootResourceHandler.java:95)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)
        at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63)
        at org.apache.wink.server.internal.handlers.HeadMethodHandler.handleRequest(HeadMethodHandler.java:53)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)
        at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63)
        at org.apache.wink.server.internal.handlers.OptionsMethodHandler.handleRequest(OptionsMethodHandler.java:46)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)
        at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63)
        at org.apache.wink.server.internal.handlers.SearchResultHandler.handleRequest(SearchResultHandler.java:33)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)
        at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63)
        at org.apache.wink.server.internal.log.ResourceInvocation.handleRequest(ResourceInvocation.java:92)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)
        at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63)
        at org.apache.wink.server.internal.log.Requests.handleRequest(Requests.java:76)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:26)
        at org.apache.wink.server.handlers.RequestHandlersChain.handle(RequestHandlersChain.java:22)
        at org.apache.wink.server.handlers.AbstractHandlersChain.doChain(AbstractHandlersChain.java:63)
        at org.apache.wink.server.handlers.AbstractHandlersChain.run(AbstractHandlersChain.java:48)
        at org.apache.wink.server.internal.RequestProcessor.handleRequestWithoutFaultBarrier(RequestProcessor.java:207)
        at org.apache.wink.server.internal.RequestProcessor.handleRequest(RequestProcessor.java:154)
        at org.apache.wink.server.internal.servlet.RestServlet.service(RestServlet.java:119)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
        at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
        at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
        at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
        at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
        at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
        at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
        at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
        at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
        at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
        at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
        at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
        at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
        at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
        at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
        at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
        at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
        at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
        at java.lang.Thread.run(Thread.java:662)
Caused by: org.hsqldb.HsqlException: statement is not in batch mode
        at org.hsqldb.error.Error.error(Unknown Source)
        at org.hsqldb.error.Error.error(Unknown Source)
        ... 88 more

【问题讨论】:

【参考方案1】:

您需要使用executeBatch 而不是executeUpdate

还要确保您的代码至少调用了一个addBatch。见http://sourceforge.net/p/hsqldb/discussion/73673/thread/926b13f4/

【讨论】:

对不起,我写错了,executeUpdate,在我的代码中,它使用了executeBatch。问题中也已更正。 你能发布你得到的错误的完整堆栈跟踪吗? 添加到问题中 正如@Maverick 在另一条评论中提到的那样,addBatch 很可能从未被调用过。见sourceforge.net/p/hsqldb/discussion/73673/thread/926b13f4 感谢您添加链接。【参考方案2】:

使用

 int affectedRows = statement.executeBatch();

executeBatch()的返回类型是int[]。

尝试更改此设置。

【讨论】:

我无法复制粘贴代码,所以这里出错了。如果您看到任何语法错误,请忽略。代码编译正常。我面临运行时错误。 你能检查批次是否为空吗?当批次为空时也会导致此异常,即批次中没有项目。检查:for(WersMapping wersMapping : wersMapList) 感谢@Maverick,我注意到我的本地存在一些数据问题,因此批处理本身是空的。

以上是关于Java.SQL 错误:Prepared Statement 不在批处理模式下的主要内容,如果未能解决你的问题,请参考以下文章

Java 和 Access - 使用 Prepared Statement 并获取日期

未找到 Java Prepared statement 错误表

带有子查询的PHP Prepared Statement变量绑定错误

PHP MySQL Prepared Statement Join 错误

Laravel:一般错误:1615 Prepared statement需要重新准备

Prepared Statement 因 DB2 SQL 错误而失败