javasqlitejdbc

Insert vector elements to database FAST


I have the following database:

CREATE TABLE person_bornYear (name, year INT, prob FLOAT);

And I have a vector with object (PersonYear) that contains person elements: String name, int year, double prob.

I try to insert the vector elements to the database line by line:

private Statement _stat;
private Connection _conn;
private PreparedStatement _prep;
for (PersonYear py : vecToInsert) {
    this.set_prep(this.get_conn().prepareStatement("INSERT into person_bornYear values (?, ?, ?);"));
    this.get_prep().setString(1, py.get_person());
    this.get_prep().setString(2, Integer.toString(py.get_year()));
    this.get_prep().setString(3, Double.toString(py.get_accuracy()));
    this.get_prep().executeUpdate();
}

And it takes 2-3 minutes (the vector contains 100K elements).

Does someone can tip me a faster way to insert the vector elements into the DB?

Thanks in advance.


Solution

  • You can execute a simple batch query exacly as in the example here: http://www.mkyong.com/jdbc/jdbc-preparedstatement-example-batch-update/