美文网首页
JDBC从入门到放弃(三)

JDBC从入门到放弃(三)

作者: 秀逼 | 来源:发表于2017-10-28 21:10 被阅读0次

本节,我们为大家介绍下JDBC的批处理问题。

向student表中插入一万条数据
package com.iotek.advance;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.iotek.common.ConnectionFactory;
import com.iotek.common.UtilsJdbc;

public class BatchTest {
    public static void main(String[] args) {
        
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        
        try {
            connection = ConnectionFactory.getConnection();
            connection.setAutoCommit(false);
            String insertSQL = "INSERT INTO student VALUES (?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(insertSQL);
            
            long time1 = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                preparedStatement.setInt(1, i+1);
                preparedStatement.setString(2, "zhangsan" + i);
                preparedStatement.setString(3, "beijing" + i);
                preparedStatement.setString(4, "male");
                preparedStatement.setInt(5, 20);
                preparedStatement.executeUpdate();
                count++;
            }
            connection.commit();
            
            long time2 = System.currentTimeMillis();
            System.out.println("insert " + count + " rows takes " + (time2 - time1) + " ms");
            
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            UtilsJdbc.close(connection, preparedStatement, null);
        }
    }
}


运行结果为:2556 ms

采用批处理方式插入数据,关键方法是addBatch()和executeBatch()
package com.iotek.advance;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.iotek.common.ConnectionFactory;
import com.iotek.common.UtilsJdbc;

public class BatchTest {
    public static void main(String[] args) {
        
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        int count = 0;
        
        try {
            connection = ConnectionFactory.getConnection();
            connection.setAutoCommit(false);
            String insertSQL = "INSERT INTO student VALUES (?,?,?,?,?)";
            preparedStatement = connection.prepareStatement(insertSQL);
            
            long time1 = System.currentTimeMillis();
            for (int i = 0; i < 10000; i++) {
                preparedStatement.setInt(1, i+1);
                preparedStatement.setString(2, "zhangsan" + i);
                preparedStatement.setString(3, "beijing" + i);
                preparedStatement.setString(4, "male");
                preparedStatement.setInt(5, 20);
                preparedStatement.addBatch();
                count++;
            }
            
            preparedStatement.executeBatch();
            connection.commit();
            
            long time2 = System.currentTimeMillis();
            System.out.println("insert " + count + " rows takes " + (time2 - time1) + " ms");
            
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            UtilsJdbc.close(connection, preparedStatement, null);
        }
    }
}

运行结果为:166 ms

由此,我们可以看到批处理仅仅占用了之前写法的零头毫秒,大大提升了效率,所以在大量处理数据库数据的时候,最后使用批处理,将参数或者是SQL语句,放到statement维护的缓存中,然后统一,在一次与数据库的连接中,统一提交,这样可以大大提升性能。

相关文章

  • JDBC从入门到放弃(三)

    本节,我们为大家介绍下JDBC的批处理问题。 向student表中插入一万条数据 运行结果为:2556 ms 采用...

  • JDBC:从入门到放弃(一)

    JDBC(JAVA数据库连接)是用于执行sql语句的api,可以为多种关系型数据库,提供统一访问,由一组java语...

  • JDBC:从入门到放弃(二)

    ConnectionFactory 在使用ConnectionFactory之前,请保证电脑上已经安装了oracl...

  • JDBC从入门到放弃(四)

    本节将为大家介绍ORM模型 ORM:对象关系映射模型,也就是把对象映射到数据库中 其中,类映射为数据库中的表,类的...

  • 再谈JDBC(从放弃到入门)

    JDBC作为JavaEE的一部分,在web项目中都直接或者间接使用其来操作数据库,无需开发人员针对某种特别的数据库...

  • 创造的乐趣

    从入门到放弃 ██████████████████████████████████████████████████...

  • 代码审计从入门到放弃(三) & phplimit

    前言 接着前面的代码审计从入门到放弃(一) & function、代码审计从入门到放弃(二) & pcrewaf ...

  • FQ:从入门到放弃(三)

    上次的FQ:从入门到放弃(二)介绍了XX-Net的部署过程中出现的一些问题和解决办法。本文介绍部分进阶技能,都是我...

  • Canvas从入门到放弃 (三)

    在慕课网上学习了 Canvas绘图详解 这门教程,写了这篇canvas教程,想和大家分享学习的过程,希望和大家共同...

  • open基础笔记

    1,OpenGL从入门到放弃01》一些基本概念 2,《OpenGL从入门到放弃02 》GLSurfaceView和...

网友评论

      本文标题:JDBC从入门到放弃(三)

      本文链接:https://www.haomeiwen.com/subject/yrwspxtx.html