• MySql 批处理


    1. 批处理

    • 批处理只针对更新(增,删,改)语句.
    • MySql 的批处理默认是关闭的, 需要在 url 中配置参数:
      jdbc:mysal://localhost:3306/mydb1?rewriteBatchedStatements=true

    2. PreparedStatement 批处理

    • PreparedStatement 对象内部有集合.
    • 使用循环疯狂的向 pstmt 中添加 sql 参数, 使用一组参数与模板就可以匹配出
      一条 sql 语句.
    • 调用它的执行批方法, 完成向数据库发送.
    // PreparedStatement 批处理
        public class Demo{
    
            public void fun() throws SQLException {
    
                // 获取 PreparedStatement 对象
                Connection con = JdbcUtils.getConnection();
                String sql = "INSERT INTO stu VALUES(?,?,?,?)";
                PreparedStatement pstmt = con.prepareStatement(sql);
    
                // 使用循环疯狂添加参数
                for(int i = 0; i< 10000; i++){
                    pstmt.setInt(1,i+1);         // 学号
                    pstmt.setString(2, "stu_"+i); // 姓名
                    pstmt.setInt(3,i);  // 年龄
                    pstmt.setString(4, i%2==0?"male":"female"); //性别
    
                    // 添加批, 这一组参数就保存到集合中.
                    pstmt.addBatch();
                }
    
                // 执行批方法, 向数据库发送 sql 语句
                pstmt.executeBatch();
            }
        }
    

    参考资料:

  • 相关阅读:
    了解jQuery
    jQuery其他关系查找方法
    jQuery中的入口函数
    漫谈《挪威的森林》
    再说变
    再说鞋
    SDG
    Apache Geode简介
    2021/11/24策略模式
    2021/11/26
  • 原文地址:https://www.cnblogs.com/linkworld/p/7620122.html
Copyright © 2020-2023  润新知