这里以MySQL数据库为例来展示四种通过jdbc实现批处理的四种方法,这里只举例了插入100条数据的写法,其他大致相同。
第一种:最简单,最好理解的(不要这样写)
(没工作经验可能会这样写,执行效率最低)
public class jdbcDemo1 {
public static void main(String[] args) throws Exception{
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("url", "user", "password");
PreparedStatement ins = null;
Date date = new Date();
System.out.println(date.getTime());
for (int i = 1; i <= 100; i++) {
String sql = "insert into t_test (id,user) values (?,?)";
ins = con.prepareStatement(sql);
ins.setInt(1, i);
ins.setInt(2, i);
ins.execute();
}
Date dateTwo = new Date();
System.out.println(dateTwo.getTime());
}
}
第二种:使用事务,不自动commit(常用的吧)
public class jdbcDemo1 {
public static void main(String[] args) throws Exception{
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("url", "user", "password");
PreparedStatement ins = null;
int COMMIT_SIZE = 200;
con.setAutoCommit(false);
PreparedStatement ins = null;
Date date = new Date();
System.out.println(date.getTime());
for (int i = 101; i <= 200; i++) {
String sql = "insert into t_test (id,user) values (?,?)";
ins = con.prepareStatement(sql);
ins.clearParameters();
ins.setInt(1, i);
ins.setInt(2, i);
ins.execute();
if (i % COMMIT_SIZE == 0) {
con.commit();
} con.commit();
}
Date dateTwo = new Date();
System.out.println(dateTwo.getTime());
}
}
第三种: executeBatch(推荐)
public class jdbcDemo1 {
public static void main(String[] args) throws Exception{
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("url", "user", "password");
PreparedStatement ins = null;
int COMMIT_SIZE = 100;
int BATCH_SIZE = 100;
String sql = "insert into t_test1 (id,data) values (?,?)";
con.setAutoCommit(false);
PreparedStatement ins = null;
ins = con.prepareStatement(sql);
Date date = new Date();
System.out.println(date.getTime());
for (int i = 101; i <= 200; i+= BATCH_SIZE) {
ins.clearBatch();
for (int j = 0; j < BATCH_SIZE; j++){
ins.setInt(1, i+j);
ins.setInt(2, j);
ins.addBatch();
}
ins.executeBatch();
if ((i + BATCH_SIZE - 1) % COMMIT_SIZE == 0) {
con.commit();
}
}
con.commit();
Date dateTwo = new Date();
System.out.println(dateTwo.getTime());
}
}
第四种:先load再commit(速度最快)
(这样写最快。但是有些小瑕疵,sql不安全,需要改写一部分)
为什么不安全是因为这个sql是拼出来的,要改成之前写的(?,?)就可以了。
public class jdbcDemo1 {
public static void main(String[] args) throws Exception{
Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("url", "user", "password");
PreparedStatement ins = null;
int COMMIT_SIZE = 400;
PreparedStatement ins =null;
con.setAutoCommit(false);
Date date = new Date();
System.out.println(date.getTime());
ins = con.prepareStatement("load data local infile '' "
+ "into table t_test fields terminated by ','");
StringBuilder sb = new StringBuilder();
for (int i = 304; i <= 404; i++) {
sb.append(i + "," + i + "\n");
if(i % commitSize == 0){
InputStream is=new ByteArrayInputStream(sb.toString()
.getBytes());
((com.mysql.jdbc.Statement) ins)
.setLocalInfileInputStream(is);
ins.execute();
con.commit();
sb.setLength(0);
}
}
InputStream is=new ByteArrayInputStream(sb.toString().getBytes());
((com.mysql.jdbc.Statement) ins).setLocalInfileInputStream(is);
ins.execute();
con.commit();
Date dateTwo = new Date();
System.out.println(dateTwo.getTime());
}
}