Skip to content

Instantly share code, notes, and snippets.

@siggemannen
Created May 21, 2025 18:42
Show Gist options
  • Save siggemannen/6e331113461d87ef27d9d9b65efdbe9e to your computer and use it in GitHub Desktop.
Save siggemannen/6e331113461d87ef27d9d9b65efdbe9e to your computer and use it in GitHub Desktop.
Bulk error gist
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Arrays;
import org.junit.Test;
public class InsertTest
{
@Test
public void test_bulk_insert() throws Exception
{
try (Connection c = DriverManager.getConnection(
"jdbc:sqlserver://localhost\\sql2022;user=user;databaseName=TestDB;useBulkCopyForBatchInsert=true;password=pass;encrypt=false;trustServerCertificate=true;");)
{
//c.createStatement().execute("drop table if exists States; CReate table States (id int identity not null, StateCode nvarchar not null)");
try (PreparedStatement ps = c.prepareStatement("insert into states (StateCode) values (?)"))
{
for (String entity: Arrays.asList("1", "L"))
{
ps.setString(1, entity);
ps.addBatch();
}
ps.executeBatch();
}
try (ResultSet rs = c.createStatement().executeQuery("select * from states");)
{
while (rs.next())
{
System.out.println(rs.getString(2));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment