Solo  当前访客:1 开始使用

MySQL大量数据插入

  1. 从连接池中获取数据库连接

  2. 使用PreparedStatement对象

    PreparedStatement pstmt = connection.prepareStatement("INSERT INTO your_table (column1, column2) VALUES (?, ?)");  
    for (int i = 0; i < dataList.size(); i++) {  
        pstmt.setString(1, dataList.get(i).getColumn1());  
        pstmt.setInt(2, dataList.get(i).getColumn2());  
        pstmt.addBatch(); // 将插入语句添加到批次中  
        if (i % 1000 == 0) { // 每1000条记录执行一次批处理,你可以根据实际需要调整这个数值  
            pstmt.executeBatch(); // 执行批处理  
            connection.commit(); // 提交事务  
        }  
    }  
    pstmt.executeBatch(); // 执行剩余的批处理  
    connection.commit(); // 提交事务
    
  3. 注意:PreparedStatement 对象赋值的时候按照顺序和数据类型进行赋值;


标题:MySQL大量数据插入
作者:temp12138
地址:https://solo.mfyzl.icu/articles/2024/03/19/1710861030910.html

标签:
新一篇: Linux的常用目录 旧一篇: MySQL在可重复度的隔离级别下的并发问题