-4

Lets say we have a web-application where support guys can go and check various information about the system. One such web-page allows user to execute select queries on certain database table. This apps works as one-place to find most of the details about the environment.

Database solution is always there and that's not required here.

I am looking for way to find out which tables a select statement is querying in Java. So is there a regular expression or any other way to find out tables.

Stefan
  • 838
  • 3
  • 13
  • 28
changed
  • 2,103
  • 8
  • 36
  • 56
  • What database are you using? The `explain` mechanism in mysql is what you may wish to explore. This is not implemented in Java but as a part of SQL. – Ed Heal Dec 13 '11 at 16:47
  • 3
    Is this from a security point of view? Why not just grant `SELECT` permissions on those certain tables? – Mike Christensen Dec 13 '11 at 16:48
  • It sounds like you're asking how to use FROM in a sql query, which shouldn't be affected by which language is sending the query. Please provide more detail. – Thomas Dec 13 '11 at 16:48

1 Answers1

2

The best way is to implement it at DB level. Databases have very good security definitions. You can define that user X can access only specific tables. Attempt to access other tables will cause exception. Just check which exception is thrown in this case and implement logic that deals with it.

Other way is to parse SQL statement and retrieve DB table names prior to executing the statement.

Take a look on following discussion.

Community
  • 1
  • 1
AlexR
  • 114,158
  • 16
  • 130
  • 208