目录
由于数据库本身就可以批量进行选择(例如 select * from customers),
删除(例如delete * from customers)。所以本章主要讲解数据库如何实现数据的批量插入
1.使用Statement批量插入数据:
package test_batch_insert;
import java.sql.Connection;
import java.sql.Statement;
/*
* 使用Statement对象插入1000条数据
* 效率太慢,不必推荐使用 花费:40993毫秒
*/
import org.junit.Test;
import myjdbc.utils.JDBCUtil;
public class TestStatementInsert {
@Test
public void testInsert() throws Exception {
Connection conn = JDBCUtil.getConnection();
Statement statement = conn.createStatement();
long t1 = System.currentTimeMillis();
for(int i=0;i<1000;i++) {
String sql = "insert into aa(name) values('name_" + i +"')";
statement.execute(sql);
}
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);
statement.close();
}
}
2.使用PreparedStatement批量插入数据:
package test_batch_insert;
/*
* 使用Statement批处理(插入)1000条数据
* 花费:40851ms
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.junit.Test;
import myjdbc.utils.JDBCUtil;
public class TestPreparedStatementInsert {
@Test
public void testInsert() throws Exception {
//获取连接
Connection conn = JDBCUtil.getConnection();
//获取PreparedStatement实例
String sql = "insert into aa(name) values( ? )";
PreparedStatement ps = conn.prepareStatement(sql);
//计算运行时间
long t1 = System.currentTimeMillis();
//执行
for(int i=1;i<=1000;i++) {
ps.setObject(1, "name2_"+i);
ps.execute();
}
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);
//关闭资源
JDBCUtil.closeResource(conn, ps);
}
}
3.在2的基础上添加Batch缓存:
package test_batch_insert;
/*
* 使用Batch缓存,攒SQL语句,然后在一起提交执行,类似于IO流中的以Byte数组传流
*
* MySQL服务器默认是关闭批处理的,我们需要一个参数,让MySQL开启批处理的支持
* 在配置文件url后面加上 ?rewriteBatchedStatements=true
*或者使用更新的MySQL驱动:mysql-connector-java-5.1.37-bin.java
*
* 花费:41590ms
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.junit.Test;
import myjdbc.utils.JDBCUtil;
public class TestPreparedStatementInsert02 {
@Test
public void testInsert() throws Exception {
//获取连接
Connection conn = JDBCUtil.getConnection();
//获取PreparedStatement实例
String sql = "insert into aa(name) values( ? )";
PreparedStatement ps = conn.prepareStatement(sql);
//计算运行时间
long t1 = System.currentTimeMillis();
//执行
for(int i=1;i<=1000;i++) {
ps.setObject(1, "name3_"+i);
//1.攒SQL
ps.addBatch();
if(i%500==0) {
//执行Batch
ps.executeBatch();
//清空Batch
ps.clearBatch();
}
}
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);
//关闭资源
JDBCUtil.closeResource(conn, ps);
}
}
4.在3的基础上增加取消自动提交数据的功能
package test_batch_insert;
/*
* 使用Batch缓存,攒SQL语句,然后在一起提交执行,类似于IO流中的以Byte数组传流
*
* 设置不允许自动提交数据 conn.setAutoCommit(false);
* 提交数据 conn.commit();
*
* 花费:440ms
*
*/
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.junit.Test;
import myjdbc.utils.JDBCUtil;
public class TestPreparedStatementInsert03 {
@Test
public void testInsert() throws Exception {
//获取连接
Connection conn = JDBCUtil.getConnection();
//设置不允许自动提交数据
conn.setAutoCommit(false);
//获取PreparedStatement实例
String sql = "insert into aa(name) values( ? )";
PreparedStatement ps = conn.prepareStatement(sql);
//计算运行时间
long t1 = System.currentTimeMillis();
//执行
for(int i=1;i<=1000;i++) {
ps.setObject(1, "name3_"+i);
//1.攒SQL
ps.addBatch();
if(i%500==0) {
//执行Batch
ps.executeBatch();
//清空Batch
ps.clearBatch();
}
}
//提交数据
conn.commit();
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);
//关闭资源
JDBCUtil.closeResource(conn, ps);
}
}