Mysql使用java代码快速插入100W数据

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class InsertDataDemo {
    static Connection conn = null;

    public static void initConn() throws ClassNotFoundException, SQLException {

        String url = "jdbc:mysql://localhost:3306/wyy_moxibustion_server?"
                + "user=root&password=HCiot151231&useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC";

        try {
            // Dynamically load mysql driver
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Successfully loaded MySQL driver");
            conn = DriverManager.getConnection(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String randomStr(int size) {
                 // Define an empty string
        String result = "";
        for (int i = 0; i < size; ++i) {
                         // Generate an int type integer between 97 ~ 122
            int intVal = (int) (Math.random() * 26 + 97);
                         // Force conversion (char) intVal Convert the corresponding value to the corresponding character, and splicing the characters
                    result = result + (char) intVal;
        }
                 // Output string
        return result;
    }


    public static void insert(int insertNum) {
        // open time
        Long begin = System.currentTimeMillis();
        System.out.println("Start Inserting Data...");
        // sql prefix
        String prefix = "INSERT INTO device_connection_log (id, device_id, host, connection_time,is_certified,create_time,update_time) VALUES ";

        try {
            // save the sql suffix
            StringBuffer suffix = new StringBuffer();
                         // Set the transaction to non-automatic commit
            conn.setAutoCommit(false);
            PreparedStatement pst = conn.prepareStatement("");
            for (int i = 1; i <= insertNum; i++) {
                // Build sql suffix
                suffix.append("(" + i + ", 1, '127.0.0.1', SYSDATE(), 1, SYSDATE(), SYSDATE()),");
            }
                         // Build a complete sql
            String sql = prefix + suffix.substring(0, suffix.length() - 1);
                         // Add execution sql
            pst.addBatch(sql);
            // perform the operation
            pst.executeBatch();
            // commit the transaction
            conn.commit();

            // close the connection
            pst.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // End Time
        Long end = System.currentTimeMillis();
        System.out.println("insert"+insertNum+" data data is completed!");
        System.out.println("Time-consuming : " + (end - begin) / 1000 + "seconds");
    }


    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        initConn();
        insert(1000000);

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值