zoukankan      html  css  js  c++  java
  • JDBC:批处理

    1、批处理:

    当要执行某条SQL语句很多次时。例如,批量添加数据;使用批处理的效率要高的多

    2、如何实现批处理

    实践:


    package com.dgd.test;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.sql.*;
    
    
    public class Test {
        public static void main(String[] args) throws SQLException, ClassNotFoundException, FileNotFoundException {
    
            //注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
    
            //url,批处理需要添加一个参数:? 符号后面,多个参数之间用 & 符号; 添加参数为  rewriteBatchedStatements=true
            String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&rewriteBatchedStatements=true";
            //获取连接
            Connection conn = DriverManager.getConnection(url, "root", "123456");
            System.out.println(conn.getClass());
    
            //编写sql语句
            String sql="INSERT INTO stu VALUES(null ,?)";
            //创建 PreparedStatement 对象
            PreparedStatement s = conn.prepareStatement(sql);
            //设置 ? 值
            for (int i = 0; i <=1000 ; i++) {
                s.setObject(1,"测试数据"+i);
    
                //添加到批处理中,先攒着,本质上(底层)有一个缓冲区。先缓冲所有的要执行的SQL语句
                s.addBatch();
            }
    
            //循环外面,执行这组批处理
            s.executeBatch();
            //如果需要返回值,需要用 int[] 数组接受
            // int[] executeBatch=s.executeBatch();
    
    
            //关闭资源
            s.close();
            conn.close();
    
    
    
         }
    }
  • 相关阅读:
    性能分析与调优思想
    python散列实现映射抽象数据类型
    python接口模拟100个用户登录
    大O记法
    linux查看操作系统版本信息
    Python招聘信息
    flask-login模块官网内容整理
    python|base|环境搭建
    echarts|map
    mysql|unsigned 与 signed 类型
  • 原文地址:https://www.cnblogs.com/lemonzhang/p/12795502.html
Copyright © 2011-2022 走看看