SqlBuilder is a library which attempts to take the pain out of generating SQL queries within Java programs. Using one programming language (Java) to generate code for another language (i.e. SQL) is always a challenge. There are always issues with escaping characters within string literals, getting spaces in the right place, and getting the parentheses to match up. And often, even after the code is debugged and fully tested, it is still very fragile. The slightest change will throw things out of balance and require another round of testing and tweaking.
SqlBuilder changes that whole scenario by wrapping the SQL syntax within very lightweight and easy to use Java objects which follow the "builder" paradigm (similar to StringBuilder). This changes many common SQL syntactical, runtime errors into Java compile-time errors! Let's dive right in to some quick examples to to see how it all works.
A fairly simple SQL select query embedded in a Java program might currently look something like this:
// assuming a variety of predefined string constants
String selectQuery = "SELECT " + T1_COL1 + "," + T1_COL2 + "," +
T2_COL1 + " FROM " + TABLE1 + " " + T1 + " INNER JOIN " +
TABLE2 + " " + T2 + " ON (" + T1_IDCOL + " = " + T2_IDCOL +
") ORDER BY " + T1_COL1;
Whenever this query is modified, you will need to make sure there are sufficient commas, parentheses, and spaces to generate the correct query (not to mention the correct columns for the given tables and the correct aliases for those tables).