jdbc连接oracle10g测试数据库插入
2015-08-04 10:08
561 查看
使用jdbc向数据库插入100000条记录,分别使用statement,PreparedStatement,及PreparedStatement+批处理3种方式进行测试:
//1.使用statement插入100000条记录
public void exec(Connection conn){
try {
Long beginTime = System.currentTimeMillis();
conn.setAutoCommit(false);//设置手动提交
Statement st = conn.createStatement();
for(int i=0;i<100000;i++){
String sql="insert into t1(id) values ("+i+")";
st.executeUpdate(sql);
}
Long endTime = System.currentTimeMillis();
System.out.println("st:"+(endTime-beginTime)/1000+"秒");//计算时间
st.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//2.使用PreparedStatement对象
public void exec2(Connection conn){
try {
Long beginTime = System.currentTimeMillis();
conn.setAutoCommit(false);//手动提交
PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");
for(int i=0;i<100000;i++){
pst.setInt(1, i);
pst.execute();
}
conn.commit();
Long endTime = System.currentTimeMillis();
System.out.println("pst:"+(endTime-beginTime)/1000+"秒");//计算时间
pst.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//3.使用PreparedStatement + 批处理
public void exec3(Connection conn){
try {
conn.setAutoCommit(false);
Long beginTime = System.currentTimeMillis();
PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");
for(int i=1;i<=100000;i++){
pst.setInt(1, i);
pst.addBatch();
if(i%1000==0){//可以设置不同的大小;如50,100,500,1000等等
pst.executeBatch();
conn.commit();
pst.clearBatch();
}
}
Long endTime = System.currentTimeMillis();
System.out.println("pst+batch:"+(endTime-beginTime)/1000+"秒");
pst.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在Oracle 10g中测试,结果:
1.使用statement耗时142秒;
2.使用PreparedStatement耗时56秒;
3.使用PreparedStatement + 批处理耗时:
a.50条插入一次,耗时5秒;
b.100条插入一次,耗时2秒;
c.1000条以上插入一次,耗时1秒;
通过以上可以得出结论,在使用jdbc大批量插入数据时,明显使用第三种方式(PreparedStatement + 批处理)性能更优。
//1.使用statement插入100000条记录
public void exec(Connection conn){
try {
Long beginTime = System.currentTimeMillis();
conn.setAutoCommit(false);//设置手动提交
Statement st = conn.createStatement();
for(int i=0;i<100000;i++){
String sql="insert into t1(id) values ("+i+")";
st.executeUpdate(sql);
}
Long endTime = System.currentTimeMillis();
System.out.println("st:"+(endTime-beginTime)/1000+"秒");//计算时间
st.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//2.使用PreparedStatement对象
public void exec2(Connection conn){
try {
Long beginTime = System.currentTimeMillis();
conn.setAutoCommit(false);//手动提交
PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");
for(int i=0;i<100000;i++){
pst.setInt(1, i);
pst.execute();
}
conn.commit();
Long endTime = System.currentTimeMillis();
System.out.println("pst:"+(endTime-beginTime)/1000+"秒");//计算时间
pst.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//3.使用PreparedStatement + 批处理
public void exec3(Connection conn){
try {
conn.setAutoCommit(false);
Long beginTime = System.currentTimeMillis();
PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)");
for(int i=1;i<=100000;i++){
pst.setInt(1, i);
pst.addBatch();
if(i%1000==0){//可以设置不同的大小;如50,100,500,1000等等
pst.executeBatch();
conn.commit();
pst.clearBatch();
}
}
Long endTime = System.currentTimeMillis();
System.out.println("pst+batch:"+(endTime-beginTime)/1000+"秒");
pst.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在Oracle 10g中测试,结果:
1.使用statement耗时142秒;
2.使用PreparedStatement耗时56秒;
3.使用PreparedStatement + 批处理耗时:
a.50条插入一次,耗时5秒;
b.100条插入一次,耗时2秒;
c.1000条以上插入一次,耗时1秒;
通过以上可以得出结论,在使用jdbc大批量插入数据时,明显使用第三种方式(PreparedStatement + 批处理)性能更优。
相关文章推荐
- 理解MySQL——复制(Replication)
- 存储过程学习文档
- 软件开发JAVA基础+JavaWeb方向+算法与数据结构学习资料我的百度网盘免费下载+优质+超全
- GOOGLE分布式数据库技术演进研究--从Bigtable、Dremel到Spanner(一)
- HANA颠覆传统数据库,内存数据库技术解析
- 引跑科技副总裁张晓东:引跑DBone数据库助力大数据建设
- 3G-安卓零基础就业班
- 求助-<关于SQL2000数据库中建标识列>
- 金融行业招聘.NET中高级开发人员好工作向朋友门分享[工作地点北京知春路地铁站附近]
- 金融行业招聘.NET中高级开发人员好工作向朋友门分享[北京知春路地铁站]
- 平时可以上一上的SQL Server的网站
- Mysql实用数据库技术视频教程「从入门到实践」
- oracle 两个日期相减,返回天-时-分-秒的函数
- 【云快讯】《Salesforce云服务“改头换面”,并将推出新的金融服务云》
- Redis基本类型和底层实现
- Oracle-表查询语句
- oracle表分区详解
- Oracle 表空间相关
- Oracle视图查询慢之统计信息收集
- oracle minus union intersect