实验3种JDBC插入数据效率

本文通过三种不同的JDBC插入方式对比了数据批量插入数据库的性能:使用Statement逐条插入、使用PreparedStatement逐条插入和使用PreparedStatement加批处理的方式批量插入。结果显示,在大数据量下,采用PreparedStatement加批处理的方法效率最高。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验3种JDBC插入数据效率

importjava.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;


public class test3 {
   
    public void test1(){
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connectionconn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger");
            conn.setAutoCommit(false);
            Statementstat=conn.createStatement();
            LongstartTime=System.currentTimeMillis();
            for(int i=0;i<10000;i++){
                stat.execute("insertinto test1 values("+i+")");
            }
            longendTime=System.currentTimeMillis();
           System.out.println(endTime-startTime);
        } catch (ClassNotFoundExceptione) {
            // TODO Auto-generated catchblock
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catchblock
            e.printStackTrace();
        }
    }
  
    public void test2(){
        try {
           Class.forName("oracle.jdbc.driver.OracleDriver");
            Connectionconn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger");
            conn.setAutoCommit(false);
            PreparedStatement stat =conn.prepareStatement("insert into test1 values(?)");
            LongstartTime=System.currentTimeMillis();
            for(int i=0;i<10000;i++){
                stat.setObject(1, i);
                stat.execute();
            }
            conn.commit();
            longendTime=System.currentTimeMillis();
           System.out.println(endTime-startTime);
        } catch (ClassNotFoundExceptione) {
            // TODO Auto-generated catchblock
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catchblock
            e.printStackTrace();
        }
    }
  
    public void test3(){
        try {
           Class.forName("oracle.jdbc.driver.OracleDriver");
            Connectionconn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","scott","tiger");
            conn.setAutoCommit(false);
            PreparedStatement stat =conn.prepareStatement("insert into test1 values(?)");
            Long startTime=System.currentTimeMillis();
            for(int i=0;i<10000;i++){
                stat.setObject(1, i);
                stat.addBatch();
            }
            stat.clearBatch();
            stat.executeBatch();
            conn.commit();
            longendTime=System.currentTimeMillis();
           System.out.println(endTime-startTime);
        } catch (ClassNotFoundExceptione) {
            // TODO Auto-generated catchblock
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catchblock
            e.printStackTrace();
        }
    }
  
    public static void main(String []aaa) throws Exception{
        new test3().test1();
        new test3().test2();
        new test3().test3();
    }

}


通过上面这个实验
结果是
Statement  3672
PreparedStatement 2391
PreparedStatement+批处理 15

效率显而易见,在大数据的情况下 第3种相当的效率

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值