用Java建立一千万条测试数据

没有去找有没有简便地方法建立测试数据,我采取自己熟练的方式去生成数据:

首先是建表:

CREATE TABLE `tb_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `status` tinyint(4) NOT NULL DEFAULT '0',
  `password` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
)

代码:

DBUtil.java

package com.loger.utils;

import java.sql.Connection;
import java.sql.DriverManager;

public class DbUtil {

    private String dbUrl="jdbc:mysql://120.79.246.xx:3306/db_test";
    private String dbUserName="xxxx";
    private String dbPassword="xxxxxxx";
    private String jdbcName="com.mysql.jdbc.Driver";
    
    /**
     * 获取数据库连接
     * @return
     * @throws Exception
     */
    public Connection getCon() throws Exception{
        Class.forName(jdbcName);
        Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
        return con;
    }
    
    /**
     * 关闭数据库连接
     * @param con
     * @throws Exception
     */
    public void closeCon(Connection con) throws Exception{
        if(con!=null){
            con.close();
        }
    }
    
    public static void main(String[] args) {
        DbUtil dbUtil=new DbUtil();
        try {
            dbUtil.getCon();
            System.out.println("数据库连接成功");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

UUIDUtils.java

package com.loger.utils;

import java.util.UUID;

/**
 * @author Loger
 * Date: 2018-07-14
 * TIme: 18:28
 * Description :
 */
public class UUIDUtils {

    public static String getUUID(){
        return UUID.randomUUID().toString().replace("-","");
    }

}

Main.java

package com.loger.main;

import com.loger.utils.DbUtil;
import com.loger.utils.UUIDUtils;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Random;

/**
 * @author Loger
 * Date: 2018-08-10
 * TIme: 13:57
 * Description :
 */
public class Main {

    public static void main(String[] args) throws Exception {
        DbUtil dbUtil = new DbUtil();
        Connection con = dbUtil.getCon();
        Statement statement = con.createStatement();
        for (int j=0;j<1000;j++){
            String sql = "INSERT INTO `db_test`.`tb_user` (`id`, `name`, `status`, `password`) VALUES ";
            String sql2 = "";
            for (int i = 0; i < 10000; i++) {
                sql2 = sql2 + "(null, '"+ UUIDUtils.getUUID()+"', "+(i%10)+", '0098asafpklelld,skkke1111//.asf///'),";
            }
            sql2 = sql2 + "(null, '878999', '7', '0098asafpklelld,skkke1111//.asf///');";
            sql = sql + sql2;
            statement.execute(sql);
        }
        con.close();
    }


}

这里采取的是拼接sql,然后批量提交。

运行,然后吃把鸡吧。

猜你喜欢

转载自www.cnblogs.com/loger1995/p/9459915.html