Are the parameter values automatically cleared after executing a batch of SQL commands with a Java PreparedStatemen?

Spread the love

Question Description

I have the following code snippet:

PreparedStatement preparedStatement = connection.prepareStatement(sql);
for (int i = 0; i < 100000; i++) {
    preparedStatement.setObject(1, someValue);
    preparedStatement.addBatch();
    if ((i + 1) % 100 == 0) {
        preparedStatement.executeBatch();
    }
}

so I want to execute one command around 100 000 times with different values. My question is: are the parameters from the PreparedStatement cleared after each call to executeBatch() or do I have to explicitly call preparedStatement.clearParameters() after calling executeBatch() in order to make sure that there will be executed only the last 100 commands?

Practice As Follows

As I can see here: http://javasourcecode.org/html/open-source/jdk/jdk-6u23/sun/jdbc/odbc/JdbcOdbcStatement.java.html

executeBatch() calls clearBatch() in the end.

But there is no guarantee for that will be exactly the same in other implementations.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.