I want to build an SQL string to do database manipulation (updates, deletes, inserts, selects, that sort of thing) – instead of the awful string concat method using millions of "+"'s and quotes which is unreadable at best – there must be a better way.
I did think of using MessageFormat – but its supposed to be used for user messages, although I think it would do a reasonable job – but I guess there should be something more aligned to SQL type operations in the java sql libraries.
Would Groovy be any good?
Best Answer
First of all consider using query parameters in prepared statements:
The other thing that can be done is to keep all queries in properties file. For example in a queries.properties file can place the above query:
Then with the help of a simple utility class:
you might use your queries as follows:
This is a rather simple solution, but works well.