JooQ – The simplest ORM library in Java

jOOQJava Object Oriented Querying, is yet another Object Relation Mapping (ORM) library in java. Why does it really need a mention, when there exists other ORMs (like Hibernate, JPA) for over many years now?

The answer is simple: Because it’s the simplest ORM library in java.

How is jOOQ simple?

jOOQ generates classes from the database schema, which are then used to construct SQL queries in the Java application code. The best part is – if the database schema changes, it does not wait for throwing runtime errors. It rather shows errors at compile time so that those can be fixed immediately with no surprises at runtime and thereby reducing maintenance cost.

It also supports highly extensible SQL and handles the database differences very well. Also, it gives you low level control over the SQL queries, without compromising on the type safety interface.

Getting started with jOOQ

Enough of bragging about jOOQ now! Let’s get our hands dirty now. Follow the below steps and I bet, you will know jOOQ in 10 mins.
Before we start, for this code walk through, we will be using MySQL database.

Step 1: Download jOOQ jars

Go to http://www.jooq.org/download/versions and select the appropriate jOOQ download for you – depending on which database you are using and your purpose of using jOOQ.

For the illustration purpose, you can use “Open Source” and get the jOOQ zip from here after registration, of course.
For this code walk through, we will be using MySQL database.

Once you get the jOOQ-3.6.4.zip, extract these 3 jars – jooq-3.6.4.jar, jooq-codegen-3.6.4.jar and jooq-meta-3.6.4.jar to a folder say – “C:\work\jOOQ”

Step 2: Download SQL connector jar

You can download MySQL connector jar here. Once the download completes, extract the mysql-connector-java-5.0.8-bin to the same folder where jOOQ files were downloaded – “C:\work\jOOQ”

Step3: Create the mysql database

You can save below sql statements as .sql file and using mysqldump, import it.

Step 4: Create the jOOQ properties file

Save the below content in a .xml file, say jooq_student.xml in the directory c:\work\jOOQ

Step 5: Generate jOOQ classes

Then run the below command by going to c:\work\jOOQ directory :

Note:
a) Note the “/” before the name of xml file in the above command. It is needed as per design.
b) The <input type=”text” /> tag in the above xml file corresponds to the name of the database. In our case, its student.
c) The tag corresponds to the directory where the jOOQ classes would be generated.

Step 6: Write the Java main class

You can verify using SQL command line, if the record was inserted into the database and I bet, it is inserted successfully!! (provided you followed above steps)

Yayy!, so there you are a jOOQ beginner who can write SQL queries in jOOQ very well!

Leave a Reply

avatar
300
  Subscribe  
Notify of