Java获取数据库表信息

    @Scheduled(fixedRate = 1000 * 60 * 60 * 24)
    private synchronized void task9999() throws Exception {
        String driver = "org.postgresql.Driver";
        String url = "jdbc:postgresql://192.168.0.71:5432/middle";
        String user = "postgres";
        String password = "postgres";

        printTableStructure(driver, url, user, password);
    }

    private static void printTableStructure(String driver, String url, String user, String password) throws Exception {
        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);

        DatabaseMetaData metaData = connection.getMetaData();
        // 获取所有表
        ResultSet tableResultSet = metaData.getTables(null, null, null, new String[]{"TABLE"});
        while (tableResultSet.next()) {
            String tableName = tableResultSet.getString("TABLE_NAME");
            System.out.println("table:" + tableName);

            // 获取表字段结构
            ResultSet columnResultSet = metaData.getColumns(null, "%", tableName, "%");
            while (columnResultSet.next()) {
                // 字段名称
                String columnName = columnResultSet.getString("COLUMN_NAME");
                // 数据类型
                String columnType = columnResultSet.getString("TYPE_NAME");
                // 字段长度
                int datasize = columnResultSet.getInt("COLUMN_SIZE");
                // 小数部分位数
                int digits = columnResultSet.getInt("DECIMAL_DIGITS");
                // 是否可为空 1代表可空 0代表不可为空
                int nullable = columnResultSet.getInt("NULLABLE");
                // 描述
                String remarks = columnResultSet.getString("REMARKS");
                System.out.println(columnName + " " + columnType + " " + datasize + " " + digits + " " + nullable + " " + remarks);
            }
            System.out.println("=================================");
        }
    }

你可能感兴趣的:(Util,demo,java)