Search code examples
javajdbcrdbms

Is there any way to check read, write information for tables generically for RDBMS databases?


I need to find RDBMS table metadata for an application. I am able to find Column Metadata, table existence using java.sql.DatabaseMetaData and row count by executing query on java.sql.Statement.

How need to check

  • boolean canRead()
  • boolean canWrite()

on table.

I found isReadOnly() method in DatabaseMetaData class. But it

Retrieves whether this database is in read-only mode.

Is there any way to check these features generically for RDBMS databases at table level?


Solution

  • You are looking for getTablePrivileges()

    Retrieves a description of the access rights for each table available in a catalog. Note that a table privilege applies to one or more columns in the table. It would be wrong to assume that this privilege applies to all columns (this may be true for some systems but is not true for all.)

    The column PRIVILEGE from the ResultSet will contain the information you are looking for. If I remember correctly you will get one row in the result for each privilege that was granted.

    As the JavaDoc for getTablePrivileges() suggests, you might need to also check getColumnPrivileges() - but that depends on your use case and the DBMS you expect to support