Skip to content

Instantly share code, notes, and snippets.

@kasramp
Last active July 4, 2023 12:23
Show Gist options
  • Save kasramp/3a0d116451e7be7b0605 to your computer and use it in GitHub Desktop.
Save kasramp/3a0d116451e7be7b0605 to your computer and use it in GitHub Desktop.
Option-5 (Unnest insertion)
public void insertNames(List<String> names) {
try (Connection connection = config.getConnection()) {
PreparedStatement statement = connection.prepareStatement("INSERT INTO student (name) SELECT * FROM UNNEST(?)");
statement.setArray(1, connection.createArrayOf("text", names.toArray(new String[names.size()])));
statement.execute();
} 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