• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parse SQL Query using java  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

user create some sql query. In query contains selected coloumns,and aggregate functions etc. I need to separate selected coloumn using java.

For Ex: SELECT EMP_NAME,EMP_NO,EMP_ADDRESS,EMP_RELION,EMP_PINCODE,EMP_MOBILE,
DECODE(EMP_NAME,'TEST', EMP_NAME,EMP_NAME) from EMP;

I am trying to parse query using StringTokenizer using "," separator but not parse in properly

How to do this. Can you give me a solution for this.
 
Sheriff
Posts: 21288
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ZQL is an SQL parser for Java.
 
lowercase baba
Bartender
Posts: 12613
50
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you need to break it apart a piece at a time. First, break apart the string bases on the SQL keywords (like "select", "from", "where" "group by", "sort by", etc). Once you have everything that is between "select" and "from", you should be able to pick that apart by the commas.
 
Rancher
Posts: 42975
76
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I bet at least one of the standard Java parser packages (Antlr, JavaCC, SableCC) comes with a predefined grammar for SQL.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!