使用PreparedStatement实现批量插入
当需要成批插入或者更新记录时。可以采用Java的批量更新机制,这 一机制允许多条语句一次性提交给数据库批量处理。
通常情况下比单 独提交处理更有效率 JDBC的批量处理语句包括下面两个方法:
addBatch(String):添加需要批量处理的SQL语句或是参数;
executeBatch():执行批量处理语句;
clearBatch():清空缓存的数据
通常我们会遇到两种批量执行SQL语句的情况: 多条SQL语句的批量处理; 一个SQL语句的批量传参;
优化1: 使用PreparedStatement替代Statement
优化2: ① 使用 addBatch() / executeBatch() / clearBatch()
② ?rewriteBatchedStatements=true&useServerPrepStmts=false
;③ 使用更新的mysql 驱动: mysql-connector-java-5.1.37-bin.jar
优化3: Connection 的 setAutoCommit(false) / commit()
/**
-
@author wangxiangli
-
@create 2021-08-08-20:36
-
使用 PreparedStatement实现批量插入数据操作
-
方式一:使用Statement,效率低,淘汰
*/
public class InsterTest {
//方式二:使用PreparedStatement
@Test
public void testInster1() {
Connection conn = null;
PreparedStatement ps = null;
try {
long start = System.currentTimeMillis();
conn = JDBCUtilsDruid1.getConnection();
String sql = “insert into goods(id,Iname)values(?,?)”;
ps = conn.prepareStatement(sql);
for (int i = 0; i <= 20000; i++) {
ps.setObject(1, i);
ps.setObject(2, “name_” + i);
ps.execute();
}
long end = System.currentTimeMillis();
System.out.println(“花费时间:” + (end - start));//20000-花费时间:73470
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
JDBCUtilsDruid1.release(ps, conn);
}}
//方式三:
/*
1.addBath(),executeBatch(),clearBatch()
2.mysql服务器默认是关闭处理的,我们需要通过一个参数,让myqsl开启批处理的支持
?rewriteBatchedStatements=true 写在配置文件url的后面
3.使用更新的mysql驱动:mysql-connector-java-5.1.37-bin.jar及以后版本
*/
@Test
public void testInster2() {
Connection conn = null;
PreparedStatement ps = null;
try {
long start = System.currentTimeMillis();
conn = JDBCUtilsDruid1.getConnection();
String sql = “insert into goods(id,Iname)values(?,?)”;
ps = conn.prepareStatement(sql);
for (int i = 0; i <= 1000000; i++) {
ps.setObject(1, i);
ps.setObject(2, “name_” + i);
//1."攒"sql
ps.addBatch();
if(i%500==0){
//2.执行batch
ps.executeBatch();
//3.清空batch
ps.clearBatch();
}} long end = System.currentTimeMillis(); System.out.println("花费时间:" + (end - start));//花费时间:2643 } catch (SQLException throwables) { //1000000--17010 throwables.printStackTrace(); } finally { JDBCUtilsDruid1.release(ps, conn); }
}
//方式四:
/*
设置连接不允许自动提交数据
*/
@Test
public void testInster3() {
Connection conn = null;
PreparedStatement ps = null;
try {
long start = System.currentTimeMillis();
conn = JDBCUtilsDruid1.getConnection();
conn.setAutoCommit(false); //设置不允许自动提交数据
String sql = “insert into goods(id,Iname)values(?,?)”;
ps = conn.prepareStatement(sql);
for (int i = 0; i <= 1000000; i++) {
ps.setObject(1, i);
ps.setObject(2, “name_” + i);
//1."攒"sql
ps.addBatch();
if(i%500==0){
//2.执行batch
ps.executeBatch();
//3.清空batch
ps.clearBatch();
}} conn.commit(); long end = System.currentTimeMillis(); System.out.println("花费时间:" + (end - start)); } catch (SQLException throwables) { //1000000--花费时间:8819 throwables.printStackTrace(); } finally { JDBCUtilsDruid1.release(ps, conn); }
}
}