java 从 spring SimpleJdbcTemplate 获取所有表名的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1780677/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:57:22  来源:igfitidea点击:

Get list of all table names from spring SimpleJdbcTemplate

javaspring-jdbc

提问by niran

Is there a way to obtain the list of all table names in the database using Spring's SimpleJdbcTemplate?

有没有办法使用 Spring 的 SimpleJdbcTemplate 获取数据库中所有表名的列表?

The database being queried is Oracle if that helps in any way. Thanks.

如果有任何帮助,被查询的数据库是 Oracle。谢谢。

回答by Jason Gritman

Spring has a DatabaseMetaDataCallbackobject that can take care of some of the boiler plate aspects of the solution that duffymo has linked to. You can then pass that object when calling JDBCUtils.extractDatabaseMetaData.

Spring 有一个DatabaseMetaDataCallback对象可以处理 duffymo 链接到的解决方案的一些样板方面。然后您可以在调用时传递该对象JDBCUtils.extractDatabaseMetaData

An example of making the same call you're trying to make with those classes can be found here.

可以在此处找到尝试使用这些类进行相同调用的示例。

Sample code from that link:

该链接中的示例代码:

Class:

班级:

class GetTableNames implements DatabaseMetaDataCallback {

        public Object processMetaData(DatabaseMetaData dbmd) throws SQLException {
            ResultSet rs = dbmd.getTables(dbmd.getUserName(), null, null, new String[]{"TABLE"});
            ArrayList l = new ArrayList();
            while (rs.next()) {
                l.add(rs.getString(3));
            }
            return l;
        }
    }

Usage:

用法:

GetTableNames getTableNames = new GetTableNames();
try {
    Object o = JdbcUtils.extractDatabaseMetaData(dataSource, getTableNames);
    System.out.println(o);
} catch (MetaDataAccessException e) {
    System.out.println(e);
}

回答by duffymo

You're always free to get java.sql.DatabaseMetaData using the Connection. There aren't any methods in SimpleJdbcTemplate to help you, but frankly there's no need.

您可以随时使用 Connection 自由获取 java.sql.DatabaseMetaData。SimpleJdbcTemplate 中没有任何方法可以帮助您,但坦率地说,没有必要。

DatabaseMetaData md = c.getMetaData();
ResultSet rs = md.getTables(null, null, "%", null);
while (rs.next()) {
  System.out.println(rs.getString("TABLE_NAME"));
}

回答by bmargulies

Query the USER_TABLES view and you will get them.

查询 USER_TABLES 视图,您将获得它们。

poke around in sqlplus, of course, to see the shape first.

在sqlplus里随便找找,当然是先看形状。