Which one will give me better performance?
Use Java simply loop the value and added to the sql string and execute the statement at once? Note that PreparedStatement is also used.
INSERT INTO tbl ( c1 , c2 , c3 ) VALUES ('r1c1', 'r1c2', 'r1c3'), ('r2c1', 'r2c2', 'r2c3'), ('r3c1', 'r3c2', 'r3c3')Use the batch execution as below.
String SQL_INSERT = "INSERT INTO tbl (c1, c2, c3) VALUES (?, ?, ?);";try ( Connection connection = database.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL_INSERT); ) { int i = 0; for (Entity entity : entities) { statement.setString(1, entity.getSomeProperty()); // ... statement.addBatch(); i++; if (i % 1000 == 0 || i == entities.size()) { statement.executeBatch(); // Execute every 1000 items. } } }