Brian's Waste of Time

Sat, 18 Feb 2006

jDBI 2.0 In Progress

jDBI has been getting some love here and there, and a recent feature request has lead to a great design change.

The first thing is that the upcoming 2.0 release is going to require JDK 1.5. Why? Because I was beaten with a stick at ApacheCon for not doing something that made total sense to do, but was not very aesthetic in the current codebase. What this really means is that you can do stuff like this in the upcoming release:

    public void testDemo() throws Exception
        Handle h =;
        h.createStatement("insert into something (id, name) values (:id, :name)")
                .setInteger("id", 1)
                .setString("name", "Brian")
        h.insert("insert into something (id, name) values (?, ?)", 2, "Eric");
        h.insert("insert into something (id, name) values (?, ?)", 3, "Erin");

        List<Something> r = h.createQuery("select id, name from something " +
                                          "where name like :name " +
                                          "order by id")
                .setString("name", "Eri%")

        assertEquals(2, r.size());
        assertEquals(2, r.get(0).getId());
        assertEquals(3, r.get(1).getId());


This illustrates a couple of the biggest changes -- there are statement and query abstractions which allow for much more fine tuning of what happens. This opens the door for lazy iteration, setting of isolation levels, etc. All the nice stuff which you need sometimes in JDBC.

The map(Something.class) in there is a fun one -- by default queries still do lists of maps (of String => Object) but bean mapping, custom mapping, etc are much more easily done with the ability to specify a mapper in the query, rather than as an interceptor on the handle. The API is much nicer there.

FInally, what isn't shown above because I haven't finished it, is the statement rewriting system. It is designed to pretty much support first-class macros, with access to the parameters at the time of macro expansion. This allows for some really interesting dynamic SQL scenarios done very cleanly.

I also thought up a byline for jDBI =) "Because SQL shouldn't be such a pain the ass in Java." Not very catchy, but it certainly sums up my thoughts.

0 writebacks [/src/java/jdbi] permanent link