Last active
July 4, 2023 07:45
-
-
Save kasramp/3855d2b1fb9a3155e8e4 to your computer and use it in GitHub Desktop.
Option-2 (Single insert with keeping a connection alive for all 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.executeUpdate(); | |
} | |
} | |
} 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