JDBC 批量插入Mysql 字段值采用随机字符 100万条数据

时间:2022-12-30 23:43:54
/**
 * Created by tonydiao on 2014/12/12.
 */

import java.sql.*;
import java.util.Random;

public class ImportToMysql {
    public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static final Random random = new Random();

    public static void main(String[] args) {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://127.0.0.1:3306/point";
        String user = "root";
        String password = "root";

        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, user, password);
            if (!conn.isClosed()) {

                System.out.println("connect to Mysql database successfully!");
            }
            conn.setAutoCommit(false);
            String sql = "insert into pw_tasklog(task_id,clientid,profileid,app_id,package_name,appkey,status,create_ip,point_amount,platform) values(?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);

            for (int i = 0; i < 1e6; i++) {

                pstmt.setString(1, generateString(42));
                pstmt.setString(2, generateString(32));
                pstmt.setString(3, generateString(32));
                pstmt.setLong(4, random.nextInt(100));
                pstmt.setString(5, generateString(20));
                pstmt.setString(6, generateString(10));
                pstmt.setInt(7, random.nextInt(2) + 1);
                pstmt.setString(8, random.nextInt(255) + "." + random.nextInt(255) + "." + random.nextInt(255) + "." + random.nextInt(255));
                pstmt.setInt(9, random.nextInt(30));
                pstmt.setInt(10, random.nextInt(2));
                pstmt.addBatch();

                if (i % 1000 == 0) {
                    pstmt.executeBatch();
                    conn.commit();
                }


            }

            conn.commit();
            conn.close();
        } catch (ClassNotFoundException e) {
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String generateString(int length) {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < length; i++) {
            sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length())));
        }
        return sb.toString();
    }

}


CREATE TABLE `pw_tasklog` (
  `task_id` varchar(128) NOT NULL DEFAULT ''  ,
  `clientid` varchar(128) NOT NULL DEFAULT '' ,
  `profileid` varchar(128) NOT NULL DEFAULT '' ,
  `app_id` bigint(20) NOT NULL DEFAULT '0' ,
  `package_name` varchar(128) NOT NULL DEFAULT '' ,
  `appkey` varchar(128) NOT NULL DEFAULT '' ,
  `status` int(10) NOT NULL DEFAULT '1' ,
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `create_ip` varchar(32) NOT NULL DEFAULT '' ,
  `point_amount` int(10) NOT NULL DEFAULT '0' ,
  `platform` int(10) NOT NULL DEFAULT '0' ,
  PRIMARY KEY (`task_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8