Java JDBC Database Metadata tableIsAccesable(Connection conn, String TableName)

Here you can find the source of tableIsAccesable(Connection conn, String TableName)

Description

table Is Accesable

License

Apache License

Declaration

protected static boolean tableIsAccesable(Connection conn, String TableName) throws SQLException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.sql.*;

public class Main {
    public static int SELECT_PRIVILEGE = 1;
    public static int INSERT_PRIVILEGE = 2;

    protected static boolean tableIsAccesable(Connection conn, String TableName) throws SQLException {
        int NeedsPrivilege = SELECT_PRIVILEGE | INSERT_PRIVILEGE;
        DatabaseMetaData md = conn.getMetaData();
        try {//from w w w .ja  va  2  s.co m
            String me = md.getUserName();
            ResultSet rs = md.getTablePrivileges(null, null, TableName);
            boolean HasResults = false;
            while (rs.next()) {
                HasResults = true;
                String Grantee = rs.getString(5); // gte the table
                String Privilege = rs.getString(6); // gte the table
                if (Privilege.equalsIgnoreCase("public"))
                    NeedsPrivilege = 0;
                if (Privilege.equalsIgnoreCase("insert"))
                    NeedsPrivilege &= ~INSERT_PRIVILEGE;
                if (Privilege.equalsIgnoreCase("select"))
                    NeedsPrivilege &= ~SELECT_PRIVILEGE;
            }
            if (NeedsPrivilege == 0)
                return (true);
            return (!HasResults); // so if no results then is OK I guess
        } catch (SQLException ex) {
            return (true); // no way to tell
        }
    }
}

Related

  1. tableExists(Connection connection, String tableName)
  2. tableExists(Connection connection, String tableName)
  3. tableExists(DatabaseMetaData metaData, String tableName)
  4. tableExists(String tableName, Connection conn)
  5. tableExists(String tableName, DatabaseMetaData dbm)