Connection获取数据库、表、字段元数据

前言

使用JDBC Connection 获取数据库信息,表信息和字段信息元数据。


提示:这里我使用 JDBC连接 ES 作为样例,其他数据源一样

一、获取数据库元数据

数据库元数据使用 connection.getMetaData().getCatalogs();

查看源码如下,可看到字段为 TABLE_CAT

    /**
     * Retrieves the catalog names available in this database.  The results
     * are ordered by catalog name.
     *
     * <P>The catalog column is:
     *  <OL>
     *  <LI><B>TABLE_CAT</B> String {@code =>} catalog name
     *  </OL>
     *
     * @return a <code>ResultSet</code> object in which each row has a
     *         single <code>String</code> column that is a catalog name
     * @exception SQLException if a database access error occurs
     */
    ResultSet getCatalogs() throws SQLException;

使用Connection 获取,代码如下,这里打印出所有数据库信息

    /**
     * 查看所有数据库.
     *
     * @param connection
     * @throws Exception
     */
    public static void showDatabases(Connection connection) throws Exception{
   
   
        ResultSet schemas = connection.getMetaData().getCatalogs();
        while (schemas.next()){
   
   
            System.out.println(schemas.getObject("TABLE_CAT"));
        }
    }

二、获取数据表元数据

查看表的元数据,是为了查询数据库里所有的表信息,如表名、表描述等等

方法是 connection.getMetaData().getTables,查看源码如下:

/**
     * Retrieves a description of the tables available in the given catalog.
     * Only table descriptions matching the catalog, schema, table
     * name and type criteria are returned.  They are ordered by
     * <code>TABLE_TYPE</code>, <code>TABLE_CAT</code>,
     * <code>TABLE_SCHEM</code> and <code>TABLE_NAME</code>.
     * <P&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林志鹏JAVA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值