Page 1 of 1

sql java batch insert use statement

PostPosted: Wed Jan 16, 2013 5:18 pm
by haretu
Code: Select all
Connection connection = new getConnection(); Statement statemenet = connection.createStatement();   for (String query : queries) {     statemenet.addBatch(query); } statemenet.executeBatch(); statemenet.close(); connection.close();


Re: sql java batch insert use statement

PostPosted: Wed Jan 16, 2013 5:20 pm
by haretu
String sql = "insert into employee (name, city, phone) values (?, ?, ?)";
Connection connection = new getConnection();
PreparedStatement ps = connection.prepareStatement(sql);
final int batchSize = 1000; int count = 0;
for (Employee employee: employees) {
ps.setString(1, employee.getName());
ps.setString(2, employee.getCity());
ps.setString(3, employee.getPhone());
ps.addBatch();
if(++count % batchSize == 0) {
ps.executeBatch();
} } ps.executeBatch();
// insert remaining records
ps.close();
connection.close();