Search code examples
javasqlapifluent

Is there any library to represent SQL queries as objects in Java code?


I was wondering if there is any library that can be used to represent SQL queries as objects in Java.

In the code I have plenty of static variables of type java.lang.String that are hand written SQL queries. I would be looking for library having a nice fluent API that allows me to represent the queries as objects rather than strings.

Example:

Query q = select("DATE", "QUOTE")
  .from("STOCKMARKET")
  .where(eq("CORP", "?"))
  .orderBy("DATE", DESC);

Solution

  • Jequel looks pretty nifty: http://www.jequel.de/

    It uses a fluent interface, so it's easy to read, almost like natural SQL (from the docs):

    SqlString sql = select(ARTICLE.OID)
                   .from(ARTICLE, ARTICLE_COLOR)
                   .where(ARTICLE.OID.eq(ARTICLE_COLOR.ARTICLE_OID)
                   .and(ARTICLE.ARTICLE_NO.is_not(NULL)));
    

    It also supports executing queries against a DataSource with parameters, so it handles creation of parameterized queries as well.