文章目录
前言
使用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&

2万+

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



