Last active
July 4, 2023 09:31
-
-
Save kasramp/21b1c98349c28860cf09 to your computer and use it in GitHub Desktop.
Option-3 (Batch insertion)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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