jdbc连接oracle10g测试数据库插入
来源:互联网 发布:手机翻牆软件 编辑:程序博客网 时间:2024/06/03 15:03
使用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 + 批处理)性能更优。
0 0
- jdbc连接oracle10g测试数据库插入
- 使用jdbc连接oracle10g数据库
- Eclipse 用JDBC连接oracle10g数据库
- Java jdbc连接oracle10g
- postgresql jdbc 连接数据库测试
- 测试数据库jdbc连接jsp
- JDBC第一版连接数据库测试
- JDBC连接oracle10g数据库-韩顺平SQLHelper类
- JDBC连接oracle10g数据库-韩顺平SQLHelper类
- JDBC连接oracle10g数据库-韩顺平SQLHelper类
- 03-JDBC连接MySQL数据库【插入数据】
- oracle10g SQLPLUS连接数据库
- PowerDesigner连接Oracle10g数据库
- PowerDesigner连接Oracle10g数据库
- PowerDesigner连接Oracle10g数据库
- jdbc成功连接数据库的测试
- JDBC连接数据库工具类以及测试
- jdbc链接数据库(oracle10g)
- oracle表分区详解
- 高并发量网站解决方案
- 多校 hdu 5305
- Android GUI之View测量
- linux 之 mysql问题
- jdbc连接oracle10g测试数据库插入
- 深入理解javascript之函数
- 南邮 OJ 1061 简单选择排序
- 欢迎使用CSDN-markdown编辑器
- 边框宽度 border-width
- JS学习2--关于预解释
- Visual Studio 2015 速递(4)——高级特性之移动开发
- 数据挖掘技术与应用浅析
- hdu5001 Walk