sql java batch insert use statement

Commonly used J2EE server side frameworks.

sql java batch insert use statement

Postby haretu » Wed Jan 16, 2013 5:18 pm

Code: Select all
Connection connection = new getConnection(); Statement statemenet = connection.createStatement();   for (String query : queries) {     statemenet.addBatch(query); } statemenet.executeBatch(); statemenet.close(); connection.close();

haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm

Re: sql java batch insert use statement

Postby haretu » Wed Jan 16, 2013 5:20 pm

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();
haretu
 
Posts: 132
Joined: Sat Jan 08, 2011 9:56 pm


Return to struts,spring,hibernate

Who is online

Users browsing this forum: No registered users and 0 guests

cron