Java实现数据批量导入mysql数据库

本文完全照搬别人的。

原文标题:Java实现数据批量导入数据库(优化速度-2种方法)

原文地址:https://blog.csdn.net/qy20115549/article/details/52699724

 原文看着更清晰,请移步原文查看。

 

连接数据库

复制代码
package db;
import java.sql.Connection;
import java.sql.DriverManager;
/*   
 *  合肥工业大学 管理学院 qianyang 1563178220@qq.com
 */
public class MySQLConnections {
    private String driver = "";
    private String dbURL = "";
    private String user = "";
    private String password = "";
    private static MySQLConnections connection = null;

    private MySQLConnections() throws Exception {
        driver = "com.mysql.jdbc.Driver";
        dbURL = "jdbc:mysql://127.0.0.1:3306/test";
        user = "root";
        password = "112233";
        System.out.println("dbURL:" + dbURL);
    }

    public static Connection getConnection() {
        Connection conn = null;
        if (connection == null) {
            try {
                connection = new MySQLConnections();
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        try {
            Class.forName(connection.driver);
            conn = DriverManager.getConnection(connection.dbURL,
                    connection.user, connection.password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
}
复制代码

 

批量处理的两种方式

第一种方式,是每5000条记录放入数据库,一次。也就是每次提交的记录都有5000条,当然最后一次可能不是。另外,一种方式,是使用默认的提交方式。两者的处理速度都不错。50000条记录,大概15秒左右。

复制代码
package db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/*   
 *  合肥工业大学 管理学院 qianyang 1563178220@qq.com
 */
public class MySQLUpdate {
    static Connection con = MySQLConnections.getConnection();
    static PreparedStatement stmt = null;
    public static int executeInsert() throws SQLException {
        int i = 0;  
        //设置批量处理的数量             
        int batchSize = 5000;       
        stmt = con.prepareStatement("insert into mysqltest (id,name) "
                + "values (?,?)");
        // 关闭事务自动提交 ,这一行必须加上
        con.setAutoCommit(false);
        for (int j = 0; j < 50005; j++){
            ++i;
            stmt.setInt(1, j);  
            stmt.setString(2, "name");  
            stmt.addBatch();  
            if ( i % batchSize == 0 ) {
                stmt.executeBatch();
                con.commit();
            }
        }
        if ( i % batchSize != 0 ) {
            stmt.executeBatch();
            con.commit();
        }
        return i;
    }

    public static  void executeInsert2() throws SQLException {
        // 关闭事务自动提交 ,这一行必须加上  
        con.setAutoCommit(false);
        stmt = con.prepareStatement("insert into mysqltest1 (id,name) "
                + "values (?,?)");
        for (int j = 0; j < 50002; j++){
            stmt.setInt(1, j);  
            stmt.setString(2, "name");
            stmt.addBatch(); 
        }
        stmt.executeBatch();
        con.commit();
        stmt.close();   
        con.close();
    }

}
复制代码

main方法

复制代码
package main;
import java.sql.SQLException;
import db.MySQLUpdate;
/*   
 *  合肥工业大学 管理学院 qianyang 1563178220@qq.com
 */
public class Test {

    public static void main(String[] args) throws SQLException {
//      long begin1 = System.currentTimeMillis(); 
//      MySQLUpdate.executeInsert();
//      long end1 = System.currentTimeMillis();  
//      System.out.println("程序运行时间为:"+(end1-begin1));

        long begin2 = System.currentTimeMillis(); 
        MySQLUpdate.executeInsert1();
        long end2 = System.currentTimeMillis();  
        System.out.println("程序运行时间为:"+(end2-begin2));
    }

}
复制代码

 

posted on   荆棘人  阅读(1669)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2018-11-13 用javascript代码拼html
2018-11-13 异步编程学习
2017-11-13 asp.net提交危险字符处理方法之一

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示