简体   繁体   English

如何获取模式的所有表名称?

[英]How can I get all tables names of a schema?

How can I get all tables names of a schema? 如何获取模式的所有表名称? I have tried this: 我已经试过了:

DatabaseMetaData metaData = (DatabaseMetaData) conn.getMetaData();
metaData.getTables(null, schema, null, null);

but it does not work. 但它不起作用。

Finally I have made this: 最后我做了这个:

conn.setCatalog(mySchema);
String sqlQuery = "show tables";
rs = ps.executeQuery(sqlQuery); 
while (rs.next())
{
   System.out.print(rs.getString(1));
}

Try the following: 请尝试以下操作:

DatabaseMetaData metaData = (DatabaseMetaData) conn.getMetaData();
ResultSet rs = metaData.getTables(null, null, "%", null);
while (rs.next()) {
  System.out.println(rs.getString(3));
}

The documentation says that the third column is TABLE_NAME . 文档说,第三列是TABLE_NAME

ResultSet tables = metaData.getTables(null, null, null, new String[] {"TABLE"});
while (tables.next()){
    System.out.print(tables.getString("TABLE_NAME") + " ");
}

EDIT: Second parameter is where you specify the schema pattern (eg "Sales"), null for all schemas. 编辑:第二个参数是您指定架构模式(例如“ Sales”)的位置,所有架构均为null。

For mysql:5.6 - 对于mysql:5.6-

To get particular tables of a particular schema, I have to pass the schema name as "catalog" parameter(1st param). 要获取特定模式的特定表,我必须将模式名称作为“ catalog”参数(第一个参数)传递。 It was ignoring, if I pass it as "schemaPattern" parameter( 2nd param). 如果我将其作为“ schemaPattern”参数(第二个参数)传递,它就忽略了。

I assumed that it has to do with the fact that "create schema' is a synonym for "create database" in mysql. https://dev.mysql.com/doc/refman/8.0/en/create-database.html 我认为这与“创建模式”是mysql中“创建数据库”的同义词有关。https://dev.mysql.com/doc/refman/8.0/zh-CN/create-database.html

DatabaseMetaData meta = (DatabaseMetaData) 

support.getConnection().getMetaData();
    rs = meta.getTables("schema_name", null, null, new String[] { "TABLE" });
    while (rs.next()) {
        String tblName = rs.getString("TABLE_NAME");
        System.out.println(tblName);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM