简体   繁体   中英

check current user has access to database

I log into mysql as root and run

GRANT SELECT (`creator`, `name`, `disguise`), SHOW VIEW ON `kit` TO 'ssc'@'localhost';

Now from my code I log in as 'ssc'@'localhost' .

Connection db = DriverManager.getConnection(..., 'ssc', ...);

How can the user 'ssc'@'localhost' check that it has the above grant?

I am thinking maybe I can execute some queries in a try / catch (SQLException) block, but perhaps there's a more elegant way.

Also if I am to take that approach I don't know what queries would be the most appropriate (fastest, least intensive).

  1. you can parse the SHOW GRANT query. here is the reference. https://dev.mysql.com/doc/refman/5.7/en/show-grants.html
  2. you can query the information_schema.USER_PRIVILEGES table. here is the reference. https://dev.mysql.com/doc/refman/5.7/en/user-privileges-table.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM