About Vlad Mihalcea

Vlad Mihalcea is a software architect passionate about software integration, high scalability and concurrency challenges.

JOOQ Facts: SQL functions made easy

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:

CREATE FUNCTION FORMAT_TIMESTAMP (IN_TIME TIMESTAMP) RETURNS CHAR
    RETURN TO_CHAR(IN_TIME, 'DD-MON-YYYY HH24:MI:SS.FF');

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:

@Resource
private DataSource localTransactionDataSource;

@Override
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:

@Autowired
private DSLContext localTransactionJooqContext;

@Override
public String formatTimestamp() {
	FormatTimestamp sqlFunction = new FormatTimestamp();
	sqlFunction.setInTime(new Timestamp(System.currentTimeMillis()));
	sqlFunction.execute(localTransactionJooqContext.configuration());
	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.

 

Reference: JOOQ Facts: SQL functions made easy from our JCG partner Vlad Mihalcea at the Vlad Mihalcea’s Blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


eight − = 4



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close