本文介绍了PreparedStatement上的ArrayOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很茫然,有人可以看到这段代码有什么问题吗?我在此驱动程序中使用sqlite: https://bitbucket.org/xerial/sqlite-jdbc/downloads
I'm at a loss here, can anyone see what's wrong with this code? I'm using sqlite with this driver:https://bitbucket.org/xerial/sqlite-jdbc/downloads
修复了我的最初错误,但有一个类似的错误
edit: fixed my initial error but have a similar one
public void insertTweets(final List<WatchedTweet> tweets) {
try {
final PreparedStatement stmt = connection.prepareStatement(
"insert into tweets(tweet_id, user_id, campaign_id, retweet_count, date) values(?,?,?,?,?)"
);
for(WatchedTweet tweet : tweets) {
stmt.setLong(1, tweet.getID());
stmt.setLong(2, tweet.getParent().getTwitterID());
stmt.setInt(3, tweet.getParent().getCampaignID());
stmt.setInt(4, tweet.getRetweetCount());
stmt.setLong(5, tweet.getAdded().getTime());
stmt.addBatch();
stmt.clearParameters();
}
stmt.executeBatch();
stmt.close();
} catch (SQLException sqe) {
sqe.printStackTrace();
}
}
我在此行遇到异常:
stmt.setLong(1, tweet.getID());
例外:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at org.sqlite.core.CorePreparedStatement.batch(CorePreparedStatement.java:110)
at org.sqlite.jdbc3.JDBC3PreparedStatement.setLong(JDBC3PreparedStatement.java:298)
at tweetserver.server.db.TwitterDB.insertTweets(TwitterDB.java:75)
at tweetserver.server.rest.TwitterBot.updateTimelineUntilDate(TwitterBot.java:138)
at tweetserver.server.rest.TwitterBot.process(TwitterBot.java:49)
at tweetserver.server.rest.TwitterBot.<init>(TwitterBot.java:44)
at tweetserver.server.Application.bootstrap(Application.java:25)
at tweetserver.server.Application.main(Application.java:20)
这就是我创建表格的方式
This is how I created the table
"create table if not exists tweets (" +
"id integer PRIMARY KEY AUTOINCREMENT," +
"tweet_id long NOT NULL," +
"user_id long NOT NULL," +
"campaign_id int NOT NULL, " +
"retweet_count int NOT NULL, " +
"date long NOT NULL)"`
推荐答案
根据 http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html#setLong(int,%20long)
parameterIndex - the first parameter is 1, the second is 2,
这篇关于PreparedStatement上的ArrayOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!