gpt4 book ai didi

java - 权限 oracle jdbc getMetaData

转载 作者:行者123 更新时间:2023-12-02 10:34:09 47 4
gpt4 key购买 nike

我正在使用 JDBC 驱动程序连接到不同的数据库类型。我的数据库的用户仅具有目录的查看权限。它适用于 hive/teradata,但不适用于 Oracle。

使用 Oracle,我可以检索架构,但不能检索表/列。

Oracle 权限:

SELECT_CATALOG_ROLE, CREATE SESSION, CONNECT

Java 代码:

DatabaseMetaData databaseMetadata = con.getMetaData(); resTables = databaseMetadata.getTables("Test_Schema", null, null, null);

但是一旦我授予表的选择权限,它就可以工作。

我错过了什么吗?

最佳答案

使用 DatabaseMetaData.getMetaData 会回复基础 View all_objects 、 all_tab_comments 和 all_synonyms 。这些 View 考虑了授予访问权限的内容。

SELECT_CATALOG_ROLE 授予对允许查询 dba_objects 的目录的访问权限。

为了执行所要求的操作,可以轻松编写 DatabaseMetaData.getMetaData().getTables(..) 的镜像 Java API 以使用 dba_xyz View 。与此类似的事情。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleDatabaseMetaData;

public class MyMetaData extends OracleDatabaseMetaData {



public MyMetaData(Connection conn) {
super((OracleConnection) conn);
}

public synchronized ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException {
// catalog isn't used
// dba view based sql

String sql = "SELECT NULL AS table_cat,"+
" o.owner AS table_schem,\n "+
" o.object_name AS table_name,\n "+
" o.object_type AS table_type,\n"+
" c.comments AS remarks\n" +
" FROM dba_objects o, dba_tab_comments c\n"+
" WHERE o.owner LIKE :1 ESCAPE '/'\n "+
" AND o.object_name LIKE :2 ESCAPE '/'\n"+
" AND o.owner = c.owner (+)\n "+
" AND o.object_name = c.table_name (+)\n";

// bind params

PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString(1, schemaPattern == null ? "%" : schemaPattern);
stmt.setString(2, tableNamePattern == null ? "%" : tableNamePattern);

return stmt.executeQuery();
}

public static void main(String[] args) throws SQLException {

String conString = "jdbc:oracle:thin:@//localhost:1521/xe";
Properties props = new Properties();
props.setProperty("user", "klrice");
props.setProperty("password", "klrice");
Connection conn = DriverManager.getConnection(conString, props);
// use this class
MyMetaData md = new MyMetaData(conn);

// test it out
ResultSet rset = md.getTables(null, "ORDS_METADATA", null, null);
while (rset.next()) {
System.out.println(rset.getString(2));
}

}

}

关于java - 权限 oracle jdbc getMetaData,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53410772/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com