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

发布时间:2023年12月25日

代码?


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<String> tables = new ArrayList<>();
    private static Integer line;


    public static void main(String[] args) {
        生成随机测试数据 t = new 生成随机测试数据();
        t.m = new Mysql8Instance("127.0.0.1", 3306, "test", "root", "123456");
        t.m.setReturnColumnName(true);
        t.tables.add("test1");
        t.line = 100;
        t.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<List<String>> query = m.query(sql);
        query.remove(0);
        StringBuffer in = new StringBuffer();
        in.append("insert into ").append(table).append(" (");
        for (List<String> 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<String> 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博客

文章来源:https://blog.csdn.net/Lxinccode/article/details/135152829
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。