//非批量处理
long l1 = System.currentTimeMillis();
AccounterServiceImpl acctServImpl = new AccounterServiceImpl();
for (int i = 0; i < 30; i++) {
//插入30条数据
acctServImpl.regester(new Accounter(0, "u"+i, "123"));
}
long l2 = System.currentTimeMillis();
System.out.println("非批处理插入30个用户所花时间:" +(l2-l1));//926
//批量处理
Connection conn = null;
Statement stat = null;
try {
conn = DbUtils2_Dbcp.getMysqlConn();
stat = conn.createStatement();
conn.setAutoCommit(false);
for (int i = 0; i < 200; i++) {
//建立200个插入的批处理
stat.addBatch("insert into accounter(accounter_name,accounter_psw) values('u"+(i)+"','123')");
}
//执行批处理
stat.executeBatch();
stat.clearBatch();
conn.commit();
..catch-finally....
long l3 = System.currentTimeMillis();
System.out.println("批处理插入200个用户所花时间:" +(l3-l2));
总结;虽然批处理任务大于非批处理,但是执行效率还是高的很的。
批处理有批处理的好处,但是批处理时一般不使用PreparementStatment。但是如果使用Statement的话就要小心sql注入问题。