mysql(48) : java生成随机测试数据


import com.alibaba.gts.flm.base.util.Mysql8Instance;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class 生成随机测试数据 {

    private static Mysql8Instance m;
    private static List tables = new ArrayList<>();
    private static Integer line;


    public static void main(String[] args) {
        m = new Mysql8Instance("127.0.0.1", 3306, "test", "root", "123456");
        m.setReturnColumnName(true);
        tables.add("test1");
        line = 100;
        handle();
    }

    private static void handle() {
        for (String table : tables) {
            run(table);
        }
    }

    private static void run(String table) {
        String db = m.getDatabase();
        if (table.contains(".")) {
            String[] strings = table.split("[.]");
            db = strings[0];
        }
        String sql = "select\n" +
                "\ttable_schema as '库名',\n" +
                "\ttable_name as '表名',\n" +
                "\tcolumn_name as '字段名',\n" +
                "\tcolumn_type as '字段类型',\n" +
                "\tcolumn_comment as '字段说明' ,\n" +
                "\tIS_NULLABLE '是否可为空'\n" +
                "from\n" +
                "\tinformation_schema.columns\n" +
                "where\n" +
                "\ttable_schema = '" + db + "'\n" +
                "\tand table_name = '" + table + "' ;";
        List> query = m.query(sql);
        query.remove(0);
        StringBuffer in = new StringBuffer();
        in.append("insert into ").append(table).append(" (");
        for (List list : query) {
            in.append("`").append(list.get(2)).append("`,");
        }
        in.delete(in.length() - 1, in.length());
        in.append("\n)values");
        for (Integer i = 1; i <= line; i++) {
            in.append("(");
            for (List list : query) {
                String rs = generate(list.get(3));
                if (rs != null) {
                    rs = "'" + rs + "'";
                } else {
                    rs = "null";
                }
                in.append(rs).append(",");
            }
            in.delete(in.length() - 1, in.length());
            in.append(")\n,");
        }
        in.delete(in.length() - 1, in.length());
        in.append(";");
        m.execute(in.toString());
        System.out.println("表[" + table + "]生成测试数据完成 size:" + line);
    }

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
    private static Random r = new Random();


    public static String generate(String columnType) {
        if (columnType.contains("bigint")) {
            return String.valueOf(r.nextInt(1000) * 10000L);
        }
        if (columnType.contains("datetime")) {
            return sdf.format(new Date());
        }
        if (columnType.contains("double")) {
            return String.valueOf(r.nextDouble() * 10000);
        }
        if (columnType.contains("varchar")) {
            return generateRandomString(4);
        }
        if (columnType.contains("int")) {
            return String.valueOf(r.nextInt(1000));
        }
        return null;
    }

    private static final String[] CHARS = new String[]{"1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H",
            "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"};

    public static String generateRandomString(int bit) {
        StringBuilder sb = new StringBuilder(20);
        Random r = new Random();
        for (int i = 0; i < bit; i++) {
            int num = r.nextInt(CHARS.length);
            sb.append(CHARS[num]);
        }
        return sb.toString();
    }
}
Mysql8Instance类如下

mysql(30) : java管理mysql8(开发用轻量版)_java mysql8-CSDN博客

你可能感兴趣的:(mysql,mysql,java,python,生成随机数据,mysql生成测试数据)