Enterprise Java

Write SQL in Java with jOOQ

jOOQ is a “database first”, type safe SQL API that allows you to write SQL in Java intuitively as if the SQL language were supported natively by the Java compiler.

All database schemas, tables, columns, procedures, and other objects are made available as Java objects that can be used directly in the jOOQ SQL API.

Let’s see how it works…

 
 
 

For instance, let’s assume your database consists of this table:

CREATE TABLE CUSTOMER (
    ID INT, 
    FIRST_NAME VARCHAR(50),
    LAST_NAME VARCHAR(50),
    AGE INT
);

When you run jOOQ’s code generator against it, then you will be able to interact with your database as follows:

dsl.select(CUSTOMER.FIRST_NAME, CUSTOMER.LAST_NAME)
   .from(CUSTOMER)
   .where(CUSTOMER.AGE.gt(20))
   .and(CUSTOMER.LAST_NAME.like("S%"))
   .fetch();

jOOQ’s main features are:

  • Database first: Your database holds your most important asset – your data. You want to be in control of your SQL.
  • Typesafe SQL: Use your IDE to write SQL efficiently in Java.
  • Code generation: Your Java compiler will detect errors early.
  • Active Records: Don’t write repetitive CRUD, just easily store modified records.

jooq-banner

But jOOQ also ships with a variety of secondary features:

  • Multi-tenancy: Configure schema and table names at runtime, and implement row-level security.
  • Standardization: Write SQL that works on all your databases without wasting time on concrete syntax.
  • Query Lifecycle: Hook into the SQL code generation lifecycle, for logging, transaction handling, ID generation, SQL transformation and much more.
  • Stored Procedures: Calling them or embedding them in your SQL is a one-liner. Don’t waste time with JDBC.

Curious? Get started with the FREE JCG Academy Course on jOOQ!

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button