public class Batch_ {
@Test
public void useBatch() throws Exception {
Connection connection = JDBCUtils.getConnection();
String sql = "insert into admin values(null, ?, ?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
long start = System.currentTimeMillis();
for (int i = 0; i < 5000; i++) {
preparedStatement.setString(1,"jack" + i);
preparedStatement.setString(2,"123456");
preparedStatement.addBatch();
if((i + 1) % 1000 == 0){
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println("用时:" + (end - start));
JDBCUtils.close(null,preparedStatement,connection);
}
}
使用Batch要在url添加语句
?rewriteBatchedStatements=true