Skip to content

Instantly share code, notes, and snippets.

@kasramp
Last active July 4, 2023 09:31
Show Gist options
  • Save kasramp/21b1c98349c28860cf09 to your computer and use it in GitHub Desktop.
Save kasramp/21b1c98349c28860cf09 to your computer and use it in GitHub Desktop.
Option-3 (Batch insertion)
public void insertStudents(List<Student> students) {
String statement = "INSERT INTO student VALUES(?,?,?)";
try (Connection connection = config.getConnection()) {
try (PreparedStatement ps = connection.prepareStatement(statement)) {
for (int i = 0; i < students.size(); i++) {
Student student = students.get(i);
int j = 0;
ps.setString(++j, student.getGuid());
ps.setString(++j, student.getName());
ps.setInt(++j, student.getId());
ps.addBatch();
if (i % 1000 == 0) {
ps.executeBatch();
}
}
ps.executeBatch();
System.out.println("Batch successfully inserted");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment