How To Execute Parameterized Insert Queries In Batch Using JDBC
Feb 10, 2024
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:
- Cover photo by Wesley Tingey on Unsplash