JDBC--批量插入二十万行数据到数据库的几种方法(不断迭代)

PreparedStatement vs Statement

1、代码的可读性和可维护性

2、PreparedStatement能最大可能提高性能

- DBServer会对预编译语句提供优化,因为预编译语句有可能被重复调用,所以在语句被DBServer的编译器编译后的代码被缓存下来,那么下次调用时只要是相同的预编译语句就不再需要再次编译,只需要将参数直接传入编译过的语句执行代码中就会得到执行

- 在statement语句中,即使是相同的操作但数据内容不一样,所以整个语句本身不能匹配,没有缓存的意义,事实是没有数据库会对普通的语句编译后的执行代码保存,这样每执行一次都要对传入的语句编译一次

- 语法检查,语义检查,翻译成二进制命令,缓存

3、PreparedStatement 可以防止sql注入

方式一,使用statement类,但是不建议使用,这里只讨论使用预编译的statement类

方式二:

//方式二
public class PreparedStatement_Insert1 {
    public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        //获取数据库连接
        Connection connection = DatabaseConnectivity.getConnection("jdbc.properties");
        String sql="INSERT INTO  goods(`name`) VALUES(?) ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        long start = System.currentTimeMillis();
        for(int i=1;i<=20000;i++){
            preparedStatement.setObject(1,"nama_"+i);
            preparedStatement.execute();
            System.out.println(i);
        }
        long end=System.currentTimeMillis();
        System.out.println("共花费的时间为:"+(end-start));
        //释放资源
        DatabaseConnectivity.releaseResources(connection,preparedStatement);
    }
}

方式三:使用缓冲技术

class PreparedStatement_Insert2{
    public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        //获取数据库连接
        Connection connection = DatabaseConnectivity.getConnection("jdbc.properties");
        String sql="INSERT INTO  goods(`name`) VALUES(?) ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        long start = System.currentTimeMillis();
        for(int i=1;i<=200000;i++){
            //填充占位符
            preparedStatement.setObject(1,"nama_"+i);
            preparedStatement.addBatch();
            if(i%1000==0){
                //缓冲sql
                preparedStatement.executeBatch();
                //清空缓冲
                preparedStatement.clearBatch();

            }
        }
        long end=System.currentTimeMillis();
        System.out.println("共花费的时间为:"+(end-start)/1000.0+"秒");
        //释放资源
        DatabaseConnectivity.releaseResources(connection,preparedStatement);
    }
}

 方式四:减少提交次数,等数据都插入完成后再提交

//方式四:减少提交次数
class PreparedStatement_Insert3{
    public static void main(String[] args) throws SQLException, IOException, ClassNotFoundException {
        //获取数据库连接
        Connection connection = DatabaseConnectivity.getConnection("jdbc.properties");
        //设置不可提交
        connection.setAutoCommit(false);
        String sql="INSERT INTO  goods(`name`) VALUES(?) ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        long start = System.currentTimeMillis();
        for(int i=1;i<=200000;i++){
            //填充占位符
            preparedStatement.setObject(1,"nama_"+i);
            preparedStatement.addBatch();
            if(i%1000==0){
                //缓冲sql
                preparedStatement.executeBatch();
                //清空缓冲
                preparedStatement.clearBatch();

            }
        }
        //提交数据
        connection.commit();
        long end=System.currentTimeMillis();
        System.out.println("共花费的时间为:"+(end-start)/1000.0+"秒");
        //释放资源
        DatabaseConnectivity.releaseResources(connection,preparedStatement);
    }
}

可以看出,方式四效率最高 ,在使用事务时,应该使用同一个连接,因为断开连接之后,就已经自动提交了

另外在这里复制一下如何开启数据据库的批处理数据功能

allowMultiQueries=true
可以在sql语句后携带分号,实现多语句执行。
可以执行批处理,同时发出多个SQL语句。

rewriteBatchedStatements=true
5.1.13以上版本的驱动,添加此参数可实现高性能的批量插入。
MySQL JDBC驱动在默认情况下会无视executeBatch()语句,把我们期望批量执行的一组sql语句拆散,一条一条地发给MySQL数据库,批量插入实际上是单条插入,直接造成较低的性能。
只有把rewriteBatchedStatements参数置为true, 驱动才会帮你批量执行SQL
另外这个选项对INSERT/UPDATE/DELETE都有效
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孔雀南飞梦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值