简体   繁体   English

使用oracle jdbc连接时如何获取数据库模式名称?

[英]How to get database schema name when using oracle jdbc connection?

I am trying to get all db tables using DatabaseMetaData.getTables() method. 我试图使用DatabaseMetaData.getTables()方法获取所有数据库表。 But this method requires database schema name pattern. 但是这种方法需要数据库模式名称模式。 Is it possible to get schema name for current db connection? 是否可以获取当前数据库连接的模式名称?

The standard schema for your current connection is the name of the user you use to log in. So if your user is SCOTT you have to use SCOTT for DatabaseMetaData.getTables() . 当前连接的标准模式是您用于登录的用户的名称。因此,如果您的用户是SCOTT ,则必须使用SCOTT for DatabaseMetaData.getTables()

You can obtain the username through DatabaseMetaData.getUserName() . 您可以通过DatabaseMetaData.getUserName()获取用户名。

But remember that the comparison of schema/username done in the JDBC driver is case-sensititve and normally usernames are in uppercase. 但请记住,在JDBC驱动程序中完成的模式/用户名的比较是区分大小写的,通常用户名是大写的。

I am not 100% sure if DatabaseMetaData.getUserName() will return the name in the correct case in all situations. 我不是100%确定DatabaseMetaData.getUserName()是否会在所有情况下以正确的大小写返回名称。 To be sure, you might want to do an upperCase() before using that value. 可以肯定的是,您可能希望在使用该值之前执行upperCase()。

Try to play with getCatalogs(). 尝试使用getCatalogs()。 This is a quick draft 这是一个快速草案

  public List<String> getDatabases(DBEnv dbEnv) {

        Connection conn = getConnection(dbEnv);
        List<String> resultSet = new ArrayList<String>();

        try {
            DatabaseMetaData metaData = conn.getMetaData();
            ResultSet res = metaData.getCatalogs();

            while (res.next()) {
                resultSet.add(res.getString("TABLE_CAT"));
            }

        } catch (SQLException e) {
            logger.error(e.toString());
        }

        return resultSet;

    }

从Java 7开始, Connection有一个getSchema方法: https//docs.oracle.com/javase/8/docs/api/java/sql/Connection.html#getSchema--

The answer unfortunately is that there are no consistent solutions. 不幸的是,答案是没有一致的解决方案。 If John has access to Sally.Table ... the query will work but getUserName() will return John and not Sally schema. 如果John可以访问Sally.Table ...查询将起作用,但getUserName()将返回John而不是Sally模式。 For Oracle the user owns their schema and while others may have access, that user ID is the default schema on that connection. 对于Oracle,用户拥有其架构,而其他人可能具有访问权限,该用户ID是该连接上的默认架构。

Further, neither getSchemaName() nor getCatalog() will return the schema name. 此外,getSchemaName()和getCatalog()都不会返回模式名称。

@horse_with_no_name has the closest answer for Oracle since a given user name is the (default) schema name unless overridden in object reference as shown. @horse_with_no_name具有最接近Oracle的答案,因为给定的用户名是(默认)模式名称,除非在对象引用中被覆盖,如图所示。

For other databases the same rules do not apply consistently. 对于其他数据库,相同的规则不一致。

You can get schema name using 您可以使用获取模式名称

Connection conn = 
DriverManager.getConnection("jdbc:oracle:thin:@server:port:SID",prop);    
DatabaseMetaData databaseMetaData = conn.getMetaData();
System.out.println("schema name >>>> "+databaseMetaData.getUserName());

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

相关问题 使用 TLS 证书连接到 Oracle 数据库的 JDBC - JDBC Connection to Oracle database using TLS Certificate 如何在JDBC与Oracle数据库中使用连接池? - How to use connection pool in jdbc with oracle database? 使用JDBC,tomcat和jpa获取oracle连接 - Get an oracle connection using JDBC, tomcat and jpa JDBC与Oracle数据库的连接错误 - JDBC Connection Error to Oracle Database 使用 oracle jdbc 模板在 java 中创建新的数据库连接 - Creation of new database connection in java using oracle jdbc template 通过 JDBC 使用 UCP(通用连接池)的 Oracle Express 数据库 - Oracle Express Database through JDBC using a UCP (Universal Connection Pool) 使用Oracle UCP连接池管理器时如何获得连接? - How to get Connection when using Oracle UCP Connection Pool Manager? 在Oracle DB中使用JDBC时如何查找连接是否真正超时或凭据是否错误? - When using JDBC with Oracle DB how to find if the connection really timed out or if credentials were wrong? tomcat:如何获取打开的jdbc:oracle连接的堆栈跟踪? - tomcat : How to get opened jdbc:oracle connection's stack trace? 如何从jdbc连接获取驱动程序类名(不是驱动程序名) - How to get driver class name (not driver name) from jdbc connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM