Skip to content
Advertisement

ArrayOutOfBoundsException on PreparedStatement

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();
    }
}

I get an exception at this line:

stmt.setLong(1, tweet.getID());

The exception:

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

Advertisement

Answer

As per the docs at 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, 
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement