GetColumnPrivilegesOracle.java Source code

Java tutorial

Introduction

Here is the source code for GetColumnPrivilegesOracle.java

Source

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;

public class GetColumnPrivilegesOracle {
    public static void main(String[] args) throws Exception {
        Connection conn = null;

        conn = getConnection();
        String catalogPattern = conn.getCatalog();
        String schemaPattern = "SYSTEM";
        String tableNamePattern = "HELP";

        ResultSet privileges = null;
        DatabaseMetaData meta = conn.getMetaData();

        // The '_' character represents any single character.
        // The '%' character represents any sequence of zero or more characters.
        privileges = meta.getTablePrivileges(catalogPattern, schemaPattern, tableNamePattern);
        while (privileges.next()) {

            String catalog = privileges.getString("TABLE_CAT");
            String schema = privileges.getString("TABLE_SCHEM");
            String tableName = privileges.getString("TABLE_NAME");
            String privilege = privileges.getString("PRIVILEGE");
            String grantor = privileges.getString("GRANTOR");
            String grantee = privileges.getString("GRANTEE");
            String isGrantable = privileges.getString("IS_GRANTABLE");

            System.out.println("table name:" + tableName);
            System.out.println("catalog:" + catalog);
            System.out.println("schema:" + schema);
            System.out.println("privilege:" + privilege);
            System.out.println("grantor:" + grantor);
            System.out.println("isGrantable:" + isGrantable);
            System.out.println("grantee:" + grantee);
            conn.close();

        }
    }

    public static Connection getConnection() throws Exception {
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:databaseName";
        String username = "system";
        String password = "password";
        Class.forName(driver); // load Oracle driver
        return DriverManager.getConnection(url, username, password);
    }

}