0

I am reading a CSV file into my java program and it has an sql query in its every record. I want to parse this (Select)SQL query to find the table names and column names mentioned in those queries.

e.g. Select age,name from Employee

Result:

---- Tables: [Employee]

---- Columns: [age,name]

NOTE: I do not want to fire the queries else I would have used ResultSetMetaData

4
  • In its most general form, this will be a nightmare. Imagine selecting columns created in a PIVOT or a MATCH_RECOGNIZE clause... Expressions involving columns (analytic functions, aggregates, col1 + col2)... What do you need this for? Commented Jan 5, 2017 at 5:05
  • may be you can try JSqlParser: github.com/JSQLParser/JSqlParser Commented Jan 5, 2017 at 6:38
  • You need to use any available oracle sql parser, tokenize input and get desired result using parser library. See if this thread helps you stackoverflow.com/questions/5735791/parser-for-oracle-sql Commented Jan 5, 2017 at 7:02
  • You may use DBMS_SQL.describe_columns Commented Jan 5, 2017 at 7:09

1 Answer 1

0

For simple SQL statements like your example above i would use some simple string methods to get the info. Example:

public static void main (String[]args) { 
    String sql = "Select age,name,adress,id from Employee where id = 3";
    String[] columns = sql.substring(sql.indexOf("Select")+7, sql.indexOf("from")-1).split(",");
    String table = sql.substring(sql.indexOf("from")+5).split(" ")[0];
    System.out.println(Arrays.toString(columns));
    System.out.println(table);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.