3

I need to parse sql statements and get column names and table names. I tried with sample code. I got table names but I am stucked to get column names for each table.

2
  • Why do you need to do that? Commented May 24, 2013 at 11:25
  • I am reading few methods in BO API, I got sql statments from those method, now using that sql i need to take table names and column names and store it in database. I am using strings to read sql Commented May 24, 2013 at 12:08

5 Answers 5

3

If you really really need to do this then you should look at using a proper parser toolkit like ANTLR. It is quite a steep learning curve but there are grammars around for SQL that others have already built.

Hand-cranking your own parser will lead you down a mess of bugs for anything but the most basic of queries.

Sign up to request clarification or add additional context in comments.

1 Comment

Can u please provide me sample java code to read data , as I am very new for ANTLR
2

What you need is an SQL parser.

You can try JSQLParser, or ZQL.

I have used both successfully for the same thing the you are trying to do.

You can try also sqlparser. This is commercial however and I have no experience using it.

Comments

1

If you are having ResultSet object then, you can read Column names and Table Names of fired Query.

by using ResultSetMetaData to get MetaData of your query (like table names and column names).

example

1 Comment

I am not using any Resultset object. I am invoking different API from those methods i am getting sql statments, Now from that sql i need to extract table and column names
0

You will need to create a parser for your SQL grammar. ANTLR allows to create such parsers but it can be quite difficult to create a grammar.

Here is a list of exisiting grammar for ANTLR http://www.antlr3.org/grammar/list.html

It contains grammar for mysql, oracle SQL that should work for you.

Comments

0

Checkout Foundation Parser http://foundationdb.github.io/sql-parser/

Also note JSQLParser is no longer on SourceForge but maintain at https://github.com/JSQLParser/JSqlParser/wiki

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.