权限oracle jdbc获取元数据

cigdeys3  于 2022-11-22  发布在  Oracle
关注(0)|答案(4)|浏览(239)

我正在使用JDBC驱动程序连接到不同的数据库类型。我的数据库用户对目录只有查看权限。它适用于hive/teradata,但不适用于Oracle。
使用Oracle,我可以检索模式,但不能检索表/列。
Oracle权限:
选择目录角色,创建会话,连接
Java程式码:
数据库元数据数据库元数据= con.getMetaData();“测试模式”为空,为空,为空,为空;
但是,一旦我给予了表的选择权限,它就起作用了。
我错过了什么吗?

tzxcd3kk

tzxcd3kk1#

使用DatabaseMetaData.getMetaData对基础视图all_objects、all_tab_comments和all_synonyms进行回复。这些视图考虑了授予访问权限的内容。
SELECT_CATALOG_ROLE会赠款与目录的存取权,以允许查询dba_objects。
要完成所要求的任务,可以很容易地编写一个Map到DatabaseMetaData.getMetaData().getTables(..)的JavaAPI来使用dba_xyz视图。

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));
            }    

    }

}
yqlxgs2m

yqlxgs2m2#

SELECT_CATALOG_ROLE允许对数据字典视图(例如select * from dba_users)具有SELECT权限,但不允许对用户表具有SELECT权限。必须显式指定对所需表的权限,例如grant select,update,delete on need_table for user_name; grant select,update,delete on DEPARTMENTS to test;
或者为所有表给予选择权限(不好做法)。

[oracle10@aktp ~]$ sqlplus / as sysdba

SQL*Plus: Release 10.2.0.4.0 - Production on Wed Nov 21 14:39:55 2018

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL> grant select any table to test;

Grant succeeded.

zu0ti5jz

zu0ti5jz3#

我在jdbc上也遇到过同样的问题。我的解决方案就是这么简单。

create or replace synonym youruser.all_objects for dba_objects;
create or replace synonym youruser.all_tables for dba_tables;
create or replace synonym youruser.all_tab_columns for dba_tab_columns;
create or replace synonym youruser.all_users for dba_users;
create or replace synonym youruser.all_constraints for dba_constraints;
create or replace synonym youruser.all_cons_columns for dba_cons_columns;

这对我很有效。

fcg9iug3

fcg9iug34#

感谢@ LotarSchin,您的解决方案如此简单,而且确实有效。在阅读了OracleJDBC驱动程序的源代码后,我有了一个更完整的同义词列表:

create user metadata_collector identified by "password";
grant connect to metadata_collector;
grant select any dictionary to metadata_collector;
create or replace synonym metadata_collector.all_objects for dba_objects;
create or replace synonym metadata_collector.all_tables for dba_tables;
create or replace synonym metadata_collector.all_tab_comments for dba_tab_comments;
create or replace synonym metadata_collector.all_tab_columns for dba_tab_columns;
create or replace synonym metadata_collector.all_col_comments for dba_col_comments;
create or replace synonym metadata_collector.all_users for dba_users;
create or replace synonym metadata_collector.all_constraints for dba_constraints;
create or replace synonym metadata_collector.all_cons_columns for dba_cons_columns;
create or replace synonym metadata_collector.all_arguments for dba_arguments;
create or replace synonym metadata_collector.all_synonyms for dba_synonyms;
create or replace synonym metadata_collector.all_col_privs for dba_col_privs;
create or replace synonym metadata_collector.all_tab_privs for dba_tab_privs;
create or replace synonym metadata_collector.all_ind_columns for dba_ind_columns;
create or replace synonym metadata_collector.all_trigger_cols for dba_trigger_cols;
create or replace synonym metadata_collector.all_indexes for dba_indexes;
create or replace synonym metadata_collector.all_types for dba_types;

相关问题