• JDBC-Batch 批量执行


    JDBC 批处理 SQL 语句

    首先在 jdbc 的 url 中加上 rewriteBatchedStatements=true,只有开启了这个 Mysql 才会执行批处理,否则还是一条一条执行

    Statement 不使用 Batch

    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    
    import java.io.InputStream;
    import java.sql.*;
    import java.util.Date;
    import java.util.Properties;
    
    public class BatchTest {
    
        private Connection connection;
        private PreparedStatement preparedStatement;
        private Statement statement;
    
        @BeforeEach
        public void start() throws Exception {
            Properties properties = new Properties();
            InputStream in = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties");
            properties.load(in);
    
            String driver = properties.getProperty("driver");
            String jdbcUrl = properties.getProperty("jdbcUrl");
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
    
            Class.forName(driver);
    
            connection = DriverManager.getConnection(jdbcUrl, user, password);
        }
    
        @AfterEach
        public void end() throws Exception {
            if (statement != null) {
                statement.close();
            }
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    
        @Test
        public void testStatement() {
            String sql = null;
            try {
                connection.setAutoCommit(false);
                statement = connection.createStatement();
                long begin = System.currentTimeMillis();
                for (int i = 0; i < 1000; i++) {
                    sql = "INSERT INTO batch_test (name) VALUES(" + i + ")";
                    statement.execute(sql);
                }
                long end = System.currentTimeMillis();
                System.out.println("Time: " + (end - begin));
                connection.commit();
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    connection.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    Statement 使用 Batch

    @Test
    public void testBatchWithStatement() {
        String sql = null;
        try {
            connection.setAutoCommit(false);
            statement = connection.createStatement();
            long begin = System.currentTimeMillis();
            for (int i = 0; i < 1000; i++) {
                sql = "INSERT INTO batch_test (name) VALUES('" + i + "')";
                statement.addBatch(sql);
            }
            statement.executeBatch();
            long end = System.currentTimeMillis();
            System.out.println("Time: " + (end - begin));
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }

    PreparedStatement 不使用 Batch

    @Test
    public void testPreparedStatement() {
        String sql = "INSERT INTO batch_test (name) VALUES(?)";
        try {
            connection.setAutoCommit(false);
            preparedStatement = connection.prepareStatement(sql);
            long begin = System.currentTimeMillis();
            for (int i = 0; i < 1000; i++) {
                preparedStatement.setString(1, "name_" + i);
                preparedStatement.executeUpdate();
            }
            long end = System.currentTimeMillis();
            System.out.println("Time: " + (end - begin));
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }

    PreparedStatement 使用 Batch

    @Test
    public void testBatchWithPreparedStatement() {
        String sql = "INSERT INTO batch_test (name) VALUES(?)";
        try {
            connection.setAutoCommit(false);
            preparedStatement = connection.prepareStatement(sql);
            long begin = System.currentTimeMillis();
            for (int i = 0; i < 1000; i++) {
                preparedStatement.setString(1, "name_" + i);
                preparedStatement.addBatch();
                // 添加SQL到一定数量就统一的执行一次,清空之前添加的 SQL
                if ((i + 1) % 300 == 0) {
                    preparedStatement.executeBatch();
                    preparedStatement.clearBatch();
                }
            }
            // 总条数不是批量数值的整数倍需要再额外的执行一次
            if (1000 % 300 != 0) {
                preparedStatement.executeBatch();
                preparedStatement.clearBatch();
            }
            long end = System.currentTimeMillis();
            System.out.println("Time: " + (end - begin));
            connection.commit();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }
    }


    https://blog.csdn.net/zhangyadick18/article/details/50294265

    https://www.jianshu.com/p/04d3d235cb9f

  • 相关阅读:
    【网络安全】十三步简单入侵个人电脑教程
    [深入学习Web安全](11)之XSS玩法
    W3bsafe]SQLmap过狗命令的利用+教程
    Python 爬虫修养-处理动态网页
    WEB站点服务器安全配置
    jboss final 7.1.1相关error以及解决方式
    “System.IO.FileNotFoundException”类型的未经处理的异常在 mscorlib.dll 中发生
    HDU 2196 Computer 树形DP经典题
    python3连接Mairadb数据库
    jQuery动画animate()的使用
  • 原文地址:https://www.cnblogs.com/jhxxb/p/10451387.html
Copyright © 2020-2023  润新知