PreparedStatement上的ArrayOutOfBoundsException

时间:2015-03-06 05:56:38

标签: java sqlite jdbc

我在这里不知所措,有人能看出这段代码有什么问题吗?我在这个驱动程序中使用sqlite: https://bitbucket.org/xerial/sqlite-jdbc/downloads

编辑:修复了我的初始错误,但有一个类似的错误

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)

这是我创建表格的方式

"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)"`

1 个答案:

答案 0 :(得分:2)

根据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,