Over a million developers have joined DZone.

JOOQ Facts: SQL Functions Made Easy

· Java Zone

Learn more about how the Java language, tools and frameworks have been the foundation of countless enterprise systems, brought to you in partnership with Salesforce.

The JDBC API has always been cumbersome and error-prone and I’ve never been too fond of using it. The first major improvement was brought by the Spring JDBC framework which simply revitalized the JDBC usage with its JdbcTemplate or the SqlFunction classes, to name a few. But Spring JDBC doesn’t address the shortcoming of using string function or input parameters names and this opened the door for type-safe SQL wrappers such as JOOQ.

JOOQ is the next major step towards a better JDBC API and ever since I started using it I knew there was no turning back. JOOQ became my number one choice for building dynamic queries and recently it became my standard SQL function wrapper.

To prove it, I will start with a simple SQL function:


While you should never ever use your database for formatting a Date, since that’s the job of your application logic, for the sake of testing let’s concentrate on the input and output variable types, since that’s where JOOQ excels over any other JDBC APIs.

With Spring this is how I’d call it:

private DataSource localTransactionDataSource;
public String formatTimestamp() {
    SqlFunction<String> sqlFunction =
        new SqlFunction<String>(localTransactionDataSource, "{ ? = call FORMAT_TIMESTAMP(?) }", new int[]{Types.TIMESTAMP});
    return (String) sqlFunction.runGeneric(new Date[]{new Date()});

This is way better than standard JDBC API but I don’t like to use String parameter names or casting the return value. Since HSQLDB doesn’t support using OUT parameters for SQL functions I cannot make use of StoredProcedure or SimpleJdbcCall which might have offered a better alternative to the SqlFunction example.

Let’s see how you can call it with JOOQ:

private DSLContext localTransactionJooqContext;
public String formatTimestamp() {
    FormatTimestamp sqlFunction = new FormatTimestamp();
    sqlFunction.setInTime(new Timestamp(System.currentTimeMillis()));
    return sqlFunction.getReturnValue();

In my opinion this is the most elegant SQL function wrapper I’ve ever used so far and that’s why it became my standard approach for calling SQL functions and procedures.

Code available on GitHub.

If you have enjoyed reading my article and you’re looking forward to getting instant email notifications of my latest posts, you just need to follow my blog.

Discover how the Force.com Web Services Connector (WSC) is a code-generation tool and runtime library for use with Force.com Web services, brought to you in partnership with Salesforce.


Published at DZone with permission of Vlad Mihalcea, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}