Java动态生成分库分表sql

public class GetSql {

private static final int DBSIZE = 2;

public static void main(String[] args) {
long begin = System.currentTimeMillis();
for (int i = 0; i < DBSIZE; i++) {
String sql = "";
System.out.println((i + 1) + "库");
for (int j = 0; j < 1000; j++) {
if (j % DBSIZE == i) {
sql += "CREATE TABLE `pis_fresh_price_"+ j +"` ("
+ "`ID` bigint(20) not null auto_increment comment '主键',"
+ " PRIMARY KEY (`ID`),"
+ "UNIQUE KEY `fresh_price_index` (`CMMDTY_CODE`,`CITY_TO`,`STORE_CODE`,`LEVEL_CODE`,`BUSINESS_UNIT`,`DIST_CHANNEL`)"
+ ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='表';\n";
}
}
System.out.println(sql);
        try {
            OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream("D:\\dev/表" + (i+1) + "库.sql"), "utf-8");
            oStreamWriter.write(sql);
            oStreamWriter.close();
            System.out.println("录入成功!");
        } catch (Exception e) {
            System.out.println(e + "录入失败!");
        }
}
long end = System.currentTimeMillis();   
        System.out.println("执行耗时:" + (end - begin) + "ms");
}


}

猜你喜欢

转载自blog.csdn.net/lxs575208196/article/details/80609405