poi+ResultSet+线程池导出数据库表结构

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值