【Java 数据库】Connections.getTables() 方法 获取数据库的元数据 - Go语言中文社区

【Java 数据库】Connections.getTables() 方法 获取数据库的元数据


Connections.getTables() 方法

官方API

Retrieves a description of the tables available in the given catalog.Only table descriptions matching the catalog, schema, tablename and type criteria are returned. They are ordered by TABLE_TYPE, TABLE_CAT, TABLE_SCHEM and TABLE_NAME.
返回值如下Each table description has the following columns:
1.TABLE_CAT String => table catalog (may be null)
2.TABLE_SCHEM String => table schema (may be null)
3.TABLE_NAME String => table name
4.TABLE_TYPE String => table type. Typical types are “TABLE”,“VIEW”, “SYSTEM TABLE”, “GLOBAL TEMPORARY”,“LOCAL TEMPORARY”, “ALIAS”, “SYNONYM”.
5.REMARKS String => explanatory comment on the table (may be null)
6.TYPE_CAT String => the types catalog (may be null)
7.TYPE_SCHEM String => the types schema (may be null)
8.TYPE_NAME String => type name (may be null)
9.SELF_REFERENCING_COL_NAME String => name of the designated"identifier" column of a typed table (may be null)
10.REF_GENERATION String => specifies how values inSELF_REFERENCING_COL_NAME are created. Values are"SYSTEM", “USER”, “DERIVED”. (may be null)

在这里插入图片描述


翻译

获取数据库的所有表:(以MySQL和Oracle为例,其他类型的数据库接触不过,不做解释)

Connection接口中提供了DatabaseMetaData接口:

提供:getTables()方法,该方法需要传进 4个参数:

String catalog, String schemaPattern, String tableNamePattern, String[] types

第一个是数据库名称,String catalog

对于MySQL,则对应相应的数据库,对于Oracle来说,则是对应相应的数据库实例,可以不填,也可以直接使用Connection的实例对象中的getCatalog()方法返回的值填充;

第二个是模式,String schemaPattern

可以理解为数据库的登录名,而对于Oracle也可以理解成对该数据库操作的所有者的登录名。对于Oracle要特别注意,其登陆名必须是大写,不然的话是无法获取到相应的数据,而MySQL则不做强制要求。

第三个是表名称,String tableNamePattern

一般情况下如果要获取所有的表的话,可以直接设置为null,如果设置为特定的表名称,则返回该表的具体信息。

第四个是类型标准,String[] types

数组形式传值,有"TABLE"、"VIEW"、"SYSTEM TABLE"、"GLOBAL TEMPORARY"、"LOCAL TEMPORARY"、"ALIAS""SYNONYM"这几个经典的类型,一般使用”TABLE”,即获取所有类型为TABLE的表

它返回一个ResultSet对象,有10列,详细的显示了表的类型,可根据需要使用:

TABLE_CAT String => 表类别(可为 nullTABLE_SCHEM String => 表模式(可为 nullTABLE_NAME String => 表名称
TABLE_TYPE String => 表类型。
REMARKS String => 表的解释性注释
TYPE_CAT String => 类型的类别(可为 nullTYPE_SCHEM String => 类型模式(可为 nullTYPE_NAME String => 类型名称(可为 nullSELF_REFERENCING_COL_NAME String => 有类型表的指定 "identifier" 列的名称(可为 nullREF_GENERATION String

代码示例

MySQL
		DatabaseMetaData metaData = conn.getMetaData();
		ResultSet rs = metaData.getTables(conn.getCatalog(), "root", null, new String[]{"TABLE"});
		while(rs.next()) {
		   System.out.println(rs.getString("TABLE_NAME"));
		}
Oracle
		DatabaseMetaData metaData = conn.getMetaData();
		ResultSet rs = metaData.getTables(conn.getCatalog(), "SCOTT", null, new String[]{"TABLE"});
		while(rs.next()) {
		    System.out.println(rs.getString("TABLE_NAME"));
		}

这两处的代码,唯一区别的是在第二个参数,Oracle不支持小写的,必须是大写的,换成小写则什么都获取不到,而MySQL则大小写俱可,不过我建议是全部使用大写,这里只是示例······


完整示例

在这里插入图片描述

  • 如果21行第一个参数传递为null的话,会打印出你所有数据库的所有表。应该写成conn.getCatalog(),才是打印你指定的数据库(由Connectionurl指定)中的表。
package cn.hanquan.jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class getMetaData {
	public static void main(String[] args) throws Exception {
		// 连接
		Connection conn = null;
		PreparedStatement ps1 = null;
		Class.forName("com.mysql.cj.jdbc.Driver");
		conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sorm?serverTimezone=UTC", "root", "123456");

		// 获取数据库元数据
		DatabaseMetaData dbmd = conn.getMetaData();
		System.out.println("conn.getCatalog() = " + conn.getCatalog());

		ResultSet tableRet = dbmd.getTables(conn.getCatalog(), "%", "%", new String[] { "TABLE" });
		
		while (tableRet.next()) {
			String tableName = (String) tableRet.getObject("TABLE_NAME");
			System.out.println("tableName:" + tableName);
		}
	}
}

输出

conn.getCatalog() = sorm
tableName:dept
tableName:emp
版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/sinat_42483341/article/details/99336286
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢