General

What is QueryJ?

QueryJ is a library useful when working with JDBC API.

QueryJ provides a way of ensuring error-free SQL statements, maintaining SQL simplicity, and allowing statement customization independent to the developer.

Features

Why would I be interested in using QueryJ when JDBC works fine, and it's something everybody already knows?

The purpose behind this library is simple: I've found unacceptable to find out that any application crashes just because a syntax error in a JDBC statement. This is the kind of human errors we can avoid.

Since it's a syntax error, it seems straight forward to use the tools that best manage them: lexers. So the approach was to delegate on the Java compiler the task of ensuring the SQL statement is valid before having to test it directly against the SQL lexer all database engines have built-in.

On top of that, my intention is to leverage the benefits of using Java, providing table field autocompletion and to respect the SQL style, so that statements are equally easy to read. Also, the prepared statements get improved by adding support not just for accessing parameters by its position, but by its semantics. Something similar is provided to access the query results.

What this library only does is a way to better manage how the SQL statement is built. It doesn't do anything else.

I like frameworks like Hibernate because they allow me to specify the query declaratively, in an external property or XML file. Does QueryJ provide something similar?

The idea of QueryJ is to ensure that SQL statements are syntax error-free. It uses the Java compiler as the validation tool. If the queries are not inside the Java code, and bypass the compiler checks, then they cannot be managed by QueryJ.

Future releases plan to provide an ANTLR-based SQL grammar to translate arbitrary queries to QueryJ. Keep in mind, though, that the purpose is *not* to provide runtime checksums or fixes: everything will be done at compile-time.

This makes QueryJ unuseful for you if you like runtime customization of queries. However, you should re-think if such approach is acceptable. Maybe their execution plan should be analyzed using the appropiate tools, before bundle them inside a Java application.

How fine does QueryJ integrate with EJB?

The answer is simple: it works fine with BMP, but it's not compatible with CMP.

The idea is to be able to protect yourself against stupid SQL syntax errors, when they appear directly in the code. Usually, J2EE application servers already provide some kind of tools to manage the CMP and to check the database queries it includes are correct, at deploy-time (that is, before runtime). Since you don't deal directly with SQL inside Java code, you cannot migrate anything to use QueryJ.

Use

How do I use QueryJ?

To use QueryJ, only its jar is required.

The unit-test bundled, SelectQueryTest, provides an easy way of checking how gets the code when you use QueryJ instead of standard JDBC:

      
        SelectQuery query = queryFactory.createSelectQuery();

        query.select(USERS.USERID);
        query.select(USERS.NAME);

        query.from(USERS);

        query.where(
                USERS.USERID.greaterThan(10)
            .and(
                    USERS.NAME.equals()
                .or(USERS.NAME.isNull())));
          
    
This is equivalent, of course, to the following SQL query:
      
        SELECT USERID, NAME
        FROM USERS 
        WHERE (USERID > 10) AND ((NAME = ?) OR (NAME is null))
          
    

Status

Which is the current status of QueryJ?

QueryJ is still under development. Therefore, it doesn't implement all features yet. However, it can be used progressively whereas it supports more queries or functions.