How To Execute Parameterized Insert Queries In Batch Using JDBC

The easiest solution to execute parameterized insert queries in batch is using PreparedStatement object.

import java.sql.PreparedStatement;

//...

connection.setAutoCommit(false);

PreparedStatement preparedStatement = connection
.prepareStatement("INSERT INTO phonebook (name, number) VALUES(?,?);");

for (PhoneBookEntry entry: entries) {
preparedStatement.setString(1, entry.getName());
preparedStatement.setString(2, entry.getNumber());
preparedStatement.addBatch();
}

preparedStatement.executeBatch();

connection.commit();

Credits:

--

--

No responses yet