SverreN
SverreN

Reputation: 177

Java & SQL - Error when batch processing, batch must be either executed or cleared


I'm reading a lot of data and want to inset them in a database. So far, not a big deal and everything was working fine using a prepared statement and firing it for every row.
Now I switched to batch processing for better performance and get an error:
java.sql.SQLException: Fehler bei Stapelverarbeitung aufgetreten: batch must be either executed or cleared
The only information I found on SO about this specific error is this question dealing with different types of sql stements for one batch.
However, that's not what I'm doing, here's the code:

    @Override
    public void addRow(String[] row) throws SQLException, ClassNotFoundException, IOException {
        PreparedStatement s = q.getStatement();
        for (int i = 0; i < row.length; i++) {
            s.setString(i + 1, row[i]);
        }
        s.addBatch();

    }

    @Override
    public void done() {
        try {
            DatabaseUtils.execute(q); //Error is thrown here!
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


Additional information: q is simple wrapper for PreparedStatements which allwos me to load it easier, after the first getStatement() call no further changes to the statement are possible. The statment is a simple INSERT like INSERT INTO TABLE1 VALUES (?, ?, ?, ?)
DatabaseUtils.execute does nothing else as query.getStatement().execute();.
Database is an Oracle Database V10.205, JDK 1.6´, Driver is ojdbc6
Can someone please explain me why the error is thrown?

Upvotes: 0

Views: 2079

Answers (1)

Jan
Jan

Reputation: 13858

I believe

DatabaseUtils.execute does nothing else as query.getStatement().execute();

is the problem here. Try changing it to query.getStatement().executeBatch() or create a new method DatabaseUtils.executeBatch that will do that.

Upvotes: 2

Related Questions