Parsing and modifying SQL statements in Java

Does anyone know of a SQL statement parser in Java that would allow you to have a view of an SQL query object, allow a change to that view, and generate an updated SQL statement back?

Regards, Christophe

+6
java sql parsing model
source share
3 answers

I would have thought that ANTLR would be able to do this.

+2
source share

Maybe you can take a look at JSqlParser .

0
source share

This demo version of Java SQL Parser does something like this:

SQL input:

SELECT A as A_Alias, B AS B_Alias FROM TABLE_X 

If you need to remove the second column "B AS B_Alias" from the selection list, just do something like this:

 columns.removeResultColumn(1); // 0 is the first column 

then you will get this new SQL (it was deleted automatically):

 SELECT A as A_Alias FROM TABLE_X 

This demo also shows how to replace a column, Add criteria (where clause), Add order order, etc.

0
source share

All Articles