Example usage for java.sql DatabaseMetaData getColumnPrivileges

List of usage examples for java.sql DatabaseMetaData getColumnPrivileges

Introduction

In this page you can find the example usage for java.sql DatabaseMetaData getColumnPrivileges.

Prototype

ResultSet getColumnPrivileges(String catalog, String schema, String table, String columnNamePattern)
        throws SQLException;

Source Link

Document

Retrieves a description of the access rights for a table's columns.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getMySqlConnection();
    System.out.println("Got Connection.");
    Statement st = conn.createStatement();
    //    st.executeUpdate("drop table survey;");
    st.executeUpdate("create table survey (id int,name varchar(30));");
    st.executeUpdate("insert into survey (id,name ) values (1,'nameValue')");

    ResultSet privileges = null;/*from   w ww.  j a  v a2  s . c om*/
    DatabaseMetaData meta = conn.getMetaData();
    // The '_' character represents any single character.
    // The '%' character represents any sequence of zero
    // or more characters.
    privileges = meta.getColumnPrivileges(conn.getCatalog(), null, "survey", "%");
    while (privileges.next()) {

        String catalog = privileges.getString("TABLE_CAT");
        String schema = privileges.getString("TABLE_SCHEM");
        String tableName = privileges.getString("TABLE_NAME");
        String dbColumn = privileges.getString("COLUMN_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("column:" + dbColumn);
        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);
    }

    st.close();
    conn.close();
}