有谁知道当我尝试将多个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/