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 发布
该博客介绍了一个Java程序,用于从数据库中批量导出所有表结构到Excel文件。程序首先创建一个线程池,然后遍历指定数据库的所有表,忽略特定的系统库。对于每个表,程序生成一个包含表名、备注、字段名、字段类型等信息的Excel工作表,并为每个表名创建超链接。此外,博客还展示了如何设置Excel单元格样式和列宽。

最低0.47元/天 解锁文章

785

被折叠的 条评论
为什么被折叠?



