package cn.code.jdbc; import java.sql.Connection; import java.sql.PreparedStatement; import org.junit.Test; import cn.JDBCUtils.com.JDBCUtils; public class NumberSix { @Test public void picuhli()throws Exception{ Connection con = JDBCUtils.getConnection(); String sql = "insert into stu values(?,?,?,?)"; PreparedStatement ps = con.prepareStatement(sql); for(int i =0;i<2000;i++){ ps.setString(1, "i"); ps.setString(2, "java"+i); ps.setInt(3,i+1); String folg = null; String flog=(i%2==0?"nan":"nv"); ps.setString(4, flog); ps.addBatch();//添加批,PreparedStatement里面有一个集合,所有添加的参数都会加到集合中 } Long start = System.currentTimeMillis(); ps.executeBatch();//执行批,与数据库产生交互将数据发送给数据库;注意开启mysql的批处理在url后面添加变量jdbc:mysql://127.0.0.1:3306/mydb1?rewriteBatchedStatements=true Long end = System.currentTimeMillis(); System.out.println(end-start); } }