We need to generate dynamic SQL in our Java app. Does anyone know a simple library to do this?
In our Java app we have a bunch of where
clause filter criteria (database column, operand, value). In other words, we could have a Date
instance that we need to use to filter a given datetime
Oracle column, a String
instance that we need to use to query a varchar2
column, an Integer
instance for a number
column, etc.
Is there a library that will take a base query (select a,b,c from d
) and some filter criteria and output sql in a valid format? By valid format, I'd mean that the String
filters would be surrounded by quotes, the Integer
values wouldn't be surrounded by quotes, the Date
values formatted using to_date
, etc. Something like this:
select a, b, c from table --base query
where
date_col > to_date('2003/07/09 13:15', 'yyyy/mm/dd HH24:MM') and --Date
int_col = 5 and --Integer
string_col = 'abc'
We weren't planning on using PreparedStatement
-s as it seems like this would be harder to use to generate dynamic sql.
Update
Thanks for the help all. I decided to write my own util class to accomplish this. And yes I'm using binding via PreparedStatements
!
I accepted Kjetil's answer as Squiggle seems to be a good little library that you could use (although I didn't actually test this out).