您的位置:首页 > 其它

使用JDBC进行批处理

2016-08-24 21:38 435 查看
在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率。

JDBC实现批处理有两种方式:

第一种方式:
Statement.addBatch(sql)


第二种方式:
PreparedStatement.addBatch()


使用Statement完成批处理

使用Statement对象添加要批量执行的SQL语句,如下:

Statement.addBatch(sql1);
Statement.addBatch(sql2);
Statement.addBatch(sql3);


执行批处理SQL语句

Statement.executeBatch();


清除批处理命令

Statement.clearBatch();


使用Statement完成批处理范例

编写测试的SQL脚本创建表。

create table testbatch
(
id varchar(40) primary key,
name varchar(40)
);


编写测试代码,如下所示:

public class Demo3 {

/*
create table testbatch ( id varchar(40) primary key, name varchar(40) );

*/

// 实现批处理的第一种方式
@Test
public void test1() throws SQLException {

Connection conn = null;
Statement st = null;
ResultSet rs = null;

try {
conn = JdbcUtils.getConnection();
String sql1 = "insert into testbatch(id,name) values('1','李阿云')"; // 1.可以发送不同的sql语句 2.发送的sql语句没有预编译
String sql2 = "update testbatch set name='李阿昀' where id='1'";

st = conn.createStatement(); // Statement内部维护了一个List集合,addBatch()方法加入的sql语句都加入到该List集合内了。
st.addBatch(sql1);
st.addBatch(sql2);

st.executeBatch(); // 返回int[] [3,4]——即第一条sql语句执行影响数据库表几行,第二条sql语句执行影响数据库表几行,...
st.clearBatch(); // 清除清除批处理命令
} finally {
JdbcUtils.release(conn, st, rs);
}

}

}


采用Statement.addBatch(sql)方式实现批处理的优缺点

采用Statement.addBatch(sql)方式实现批处理:

优点:可以向数据库发送多条不同的SQL语句。

缺点:

SQL语句没有预编译。

当向数据库发送多条语句相同,但仅参数不同的SQL语句时,需重复写上很多条SQL语句。例如:

Insert into user(name,password) values(‘aa’,’111’);
Insert into user(name,password) values(‘bb’,’222’);
Insert into user(name,password) values(‘cc’,’333’);
Insert into user(name,password) values(‘dd’,’444’);


使用PreparedStatement完成批处理

使用PreparedStatement完成批处理范例

public class Demo3 {

/*
create table testbatch ( id varchar(40) primary key, name varchar(40) );

*/
// 实现批处理的第二种方式
@Test
public void test2() throws SQLException {

long starttime = System.currentTimeMillis();
Connection conn = null;
PreparedStatement st = null;
ResultSet rs = null;

try {
conn = JdbcUtils.getConnection();
String sql1 = "insert into testbatch(id,name) values(?,?)"; // 1.适合做批量插入和批量更新 2.发送的sql语句都预编译过,性能会好一点。实际开发里,此种方式用到的多一点。
st = conn.prepareStatement(sql1);

/*
st.setString(1, "1");
st.setString(2, "李阿云");
st.addBatch(); // 把PreparedStatement对象内部封装的sql语句加入到PreparedStatement对象内部的List集合中

st.setString(1, "2");
st.setString(2, "叶一");
st.addBatch(); // 此时PreparedStatement对象内部的List集合中有两条sql语句
*/

// 批处理一千万条sql语句,会出现OutOfMemoryError:Java heap space——内存溢出
/*
for (int i = 1; i <= 10000000; i++) {
st.setString(1, i+"");
st.setString(2, "叶一"+i);
st.addBatch();
}
st.executeBatch();
*/
for (int i = 1; i <= 10000006; i++) {
st.setString(1, i+"");
st.setString(2, "叶一"+i);
st.addBatch();

if (i%1000==0) {
st.executeBatch();
st.clearBatch();
}
st.executeBatch(); // 剩下不到1000条sql语句也需要执行
}
} finally {
JdbcUtils.release(conn, st, rs);
}

long endtime = System.currentTimeMillis();
System.out.println("共花了:"+(endtime-starttime)/1000+"秒"); // 向MySQL数据库批插入1000万条记录,大概需要3个多小时。而Oracle数据库测试大概需要380秒
}
}


采用PreparedStatement.addBatch()方式实现批处理的优缺点

采用PreparedStatement.addBatch()实现批处理

优点:发送的是预编译后的SQL语句,执行效率高。

缺点:只能应用在SQL语句相同,但参数不同的批处理中。因此此种形式的批处理经常用于在同一个表中批量插入数据,或批量更新表的数据。

关于JDBC批处理的内容就总结这么多。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: