动态检索数据库模式

retrieve database schema dynamically

我想为我的应用程序编写一个备份工具,并且必须使用 JAVA 1.6。在我的应用程序中有几个数据库,如 derby,mySQL .. 此外还有不同的数据库模式名称。

我正在使用 JPA 和 Eclipselink。

到目前为止,我编写了一个方法来创建具有特定模式名称的所有表的列表:

    private static List<String> getTableNames(String schema, Connection connection) throws SQLException {
    final String TABLE_NAME = "TABLE_NAME";
    List<String> tableNames = new ArrayList<>();
    DatabaseMetaData dbmd = connection.getMetaData();
    ResultSet tablesRS = dbmd.getTables(null, schema, null, null);
    while (tablesRS.next()) {
        tableNames.add(tablesRS.getString(TABLE_NAME));
    }

    return tableNames;
}

这行得通,但我不知道如何获得实际的架构名称。我以为我可以通过 EntityManager 获取架构。但是我没有找到任何解决方案。有没有办法通过 EntityManager 获取我的数据库的模式名称? 有没有更好的方法来动态检索适用于不同数据库的所有表的内容?

注意,我必须使用 Java 1.6。

我知道自 JAVA 1.7 以来,connection 对象中存在一个方法 getSchema()

我刚刚在 Java Code Geek 找到了解决我的问题的方法。我以为我必须使用架构来获取所有表。事实并非如此:

public static ArrayList getTablesMetadata() throws SQLException {
    String table[] = { "TABLE" };
    ResultSet rs = null;
    ArrayList tables = null;
    // receive the Type of the object in a String array.
    rs = metadata.getTables(null, null, null, table);
    tables = new ArrayList();
    while (rs.next()) {
        tables.add(rs.getString("TABLE_NAME"));
    }
    return tables;
}