package jdbc.demo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import jdbc.utils.JdbcUtils;
import org.junit.Test;
/**
* 批处理,在url后加?rewriteBatchedStatements=true开启mysql的批处理
* @author zzh
*
*/
public class BatchDemo {
@Test
public void demo() throws Exception{
Connection con = JdbcUtils.getConnection();
String sql = "insert into stu values (?,?,?,?)";
PreparedStatement pstmt = con.prepareStatement(sql);
for(int i = 0; i < 1000; i++ ){
pstmt.setInt(1, i+1);
pstmt.setString(2, "stu_"+i);
pstmt.setInt(3, i);
pstmt.setString(4, i%2==0?"female":"male");
pstmt.addBatch(); //增加批处理
}
long start =System.currentTimeMillis();
pstmt.executeBatch(); //执行批处理
long end =System.currentTimeMillis();
System.out.println(end-start);
}
}
还没有评论,来说两句吧...