public class MainTest {
private static ThreadPoolExecutor pool = new ThreadPoolExecutor(
5,
20,
8,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(5),
Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()
);
public static void main(String[] args) {
String extSchema = "";
String driver = "com.mysql.cj.jdbc.Driver";
//为了保密url暂时不写了
String url = "";
String user = "";
String password = "";
printTableStructure(driver, url, user, password,extSchema);
}
/**
* 打印所有表结构
*
* @param driver driver
* @param url url
* @param user user
* @param password password
* @throws Exception exception
*/
private static void printTableStructure(String driver, String url, String user, String password,String extSchema){
try{
Class.forName(driver);
Connection connection = DriverManager.getConnection(url, user, password);
DatabaseMetaData metaData = connection.getMetaData();
ResultSet schemaResultSet = metaData.getCatalogs();
//同步单个库
if(!StringUtils.isEmpty(extSchema)){
generateFile(connection,metaData,extSchema);
}else{
Long start = System.currentTimeMillis();
//CountDownLatch latch = new CountDownLatch(18);
//同步所有库
while(schemaResultSet.next()){
String schema = schemaResultSet.getString("TABLE_CAT");
if("information_schema".equals(schema) ||
"seata".equals(schema) ||
"szcgc".equals(schema) ||
"szcgc_project".equals(schema)){
continue;
}
//pool.execute(() -> {
generateFile(connection,metaData,schema);
//latch.countDown();
//});
}
//pool.shutdown();
/*try{
latch.await();
}catch (InterruptedException exception){
exception.getMessage();
}*/
System.out.println("同步耗时: "+ (System.currentTimeMillis() - start));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
private static void generateFile(Connection connection,DatabaseMetaData metaData,String schema){
try{
// 获取所有表
Re
poi+ResultSet+线程池导出数据库表结构
最新推荐文章于 2024-02-09 15:14:02 发布