java - 使用PreparedStatement时出现IndexOutOfBounds

标签 java jdbc transactions prepared-statement

有谁知道当我尝试将多个PreparedStatement放入单个数据库事务时为什么会得到ArrayIndexOutOfBounds -1?当我尝试设置准备好的语句中的第一项时,它就发生了。这违背了我读过的所有内容,而且似乎无法弄清楚。

  PreparedStatement stmt = null;
    Connection conn = null;


        // Fetch the query and the request out of the QueryRequest object
        conn = cpds.getConnection();
        conn.setAutoCommit(false);

        for (PrepStatementHolder a : query)
        {
            try
            {
                stmt = conn.prepareStatement("INSERT OR IGNORE INTO data(sampleTimestamp, data) values(?,?)");
                stmt.setLong(0, a.getTimestamp());
                stmt.setBytes(1, a.getBinaryData());
                stmt.executeQuery();
            } catch (Exception e)
            {

                e.printStackTrace();
                errorLog.error("Query: " + a.getQuery() + " Timestamp " + a.getTimestamp() + " Data " + a.getBinaryData());
                return false;

            }
        }

        conn.commit();
        stmt.close();
        conn.close();

        return true;

请求的堆栈:

    java.sql.SQLException: An SQLException was provoked by the following failure: ja
va.lang.ArrayIndexOutOfBoundsException: -1
        at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
        at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:77)
        at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:74)
        at com.mchange.v2.c3p0.impl.NewPooledConnection.handleThrowable(NewPoole
dConnection.java:505)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.setLong(NewProxyPr
eparedStatement.java:184)
        at com.joy.database.DBRunnable.dbCallInsert(DBRunnable.java:293)
        at com.joy.database.DBRunnable.run(DBRunnable.java:129)
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
        at java.util.concurrent.FutureTask.run(Unknown Source)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.
access$201(Unknown Source)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.
run(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
        at org.sqlite.core.CorePreparedStatement.batch(CorePreparedStatement.jav
a:121)
        at org.sqlite.jdbc3.JDBC3PreparedStatement.setLong(JDBC3PreparedStatemen
t.java:331)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.setLong(NewProxyPr
eparedStatement.java:170)
        ... 9 more

最佳答案

预备语句的索引从 1 开始,而不是 0

stmt.setLong(0, a.getTimestamp());
stmt.setBytes(1, a.getBinaryData());

应该是

stmt.setLong(1, a.getTimestamp());
stmt.setBytes(2, a.getBinaryData()); 

关于java - 使用PreparedStatement时出现IndexOutOfBounds,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49324009/

相关文章:

mysql - 如何将mysql表中的数据转换为Insert语句

java - Java应用程序中的协议(protocol)违反异常

spring - 如何使用 Spring Integration 仅在事务成功完成时发送消息?

java - Spring Transaction - noRollbackFor 在发生异常时不会提交

java - 没有基类访问的 hibernate 字段继承

java - 用于列出具有部分 uuid 的对象的 S3 前缀

java - 两个长值相减时相差一个小时

java - 隐藏空父项 - JFace 的 TreeViewer

java - 为什么在尝试对 ResultSet 中检索到的列的值求和时出现错误?

java - 事务未激活异常 - EJB 事务状态