MySQL批量插入100万数据sql语句拼接

public class Test {

    //CREATE TABLE `t_demo` (
    //  `id` bigint(20) NOT NULL AUTO_INCREMENT,
    //  `a` char(5) DEFAULT NULL,
    //  `b` char(10) DEFAULT NULL,
    //  PRIMARY KEY (`id`)
    //) ENGINE=InnoDB AUTO_INCREMENT=1000005 DEFAULT CHARSET=utf8;
    public static void main(String[] args) throws IOException {
        for (int x = 1; x <= 1000; x++) {
            StringBuilder sql = new StringBuilder("INSERT INTO `t_demo`(a, b) VALUES ");
            for (int i = 1; i <= 999; i++) {
                splice(sql, ",");
            }
            splice(sql, ";");
            sql.append("\r\n");
            //System.out.println(sql);
            File file = new File("E:/demo.sql");
            FileWriter fw = new FileWriter(file, true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(sql.toString());
            bw.close();
            fw.close();
        }
    }

    private static void splice(StringBuilder sql, String s) {
        String value = "('%s', '%s')";
        String a = RandomStringUtils.random(5, true, false);
        String b = RandomStringUtils.random(10, true, false);
        sql.append(String.format(value, a, b)).append(s);
    }
}

 


版权声明:本文为Anenan原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。