DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • Tracing the Agentic Loop: Monitoring Multi-Round-Trip MCP Calls With OpenTelemetry
  • Retiring a Tier-0 Legacy Database Without Breaking the Business
  • The Aggregate Reference Problem

Trending

  • How to Tell If Code Is AI-Generated (and What to Ask)
  • Why GPUs Run at 40% in LLM Inference and How To Fix It
  • Testing Asynchronous Systems, Part 1: Mapping the System and Classifying Its Asynchrony
  • Building a Claude Code Observability Dashboard With Grafana and Microsoft Fabric

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Database Automation with JOOQ

Database Automation with JOOQ

This article will look at how we can automate database interaction in End 2 End automation using the JOOQ library in Java.

By 
Ravi Kumar
·
Sep. 20, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
3.5K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

This article will look at how we can automate database interaction in End 2 End automation using the JOOQ library in Java.

When we do End 2 End automation of an application, we generally consider 2 parts.

  1.  Input part : ( Entry point of an application like UI front-end.) 
  2.  Output part : (Storage point of an application like database back-end.)

The whole application looks like this:

End 2 End automation of an application.

The middle part is the business layer which we generally don't consider in End 2 End automation.

In the End 2End automation perspective, users input the data through the UI front end, and business logic gets that data to perform all the business operations. The final output data is stored in the database. In an automation script, we have to automate the input part and output verification parts.

Our end objective would be to validate the output data with expected data so that we have to first fetch the data from the database to validate; here JOOQ library helps us to interact with the database in a simple and easier way. 

There are a lot of tools in the market to help the input UI automation means the UI front-end application but the options are very limited when we have to automate the output database end of the application.

JOOQ library helps us with the automation database part.

Using the JOOQ library, we can easily write the database interaction code that we used in End 2 End application automation.

In the below section, I will demonstrate the database interaction code using JOOQ.

Prerequisites:
1. We need a database with some application data.
For this example, we are using the H2 database, so please download and install the H2 database based on your operating system.

2. Create a dummy table and dummy data using the below SQL script:
 
CREATE TABLE Persons (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255)
);

INSERT INTO PERSONS (PersonID, LastName, FirstName, Address,City) VALUES (1, 'TOM', 'CHERRY', 'NOIDA','DELHI');
INSERT INTO PERSONS (PersonID, LastName, FirstName, Address, City) VALUES (2, 'TOMMY', 'SUNNY', 'NOIDA','DELHI');


H2 database:

H2 database

3. Create a maven java project and add the below dependencies:

 
<dependency>
            <groupId>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>1.21</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>2.1.214</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jooq/jooq -->
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>jooq</artifactId>
            <version>3.17.3</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.0</version>
            <scope>test</scope>
        </dependency>  


JOOQ for Database Automation

To connect the database, we have to first create a file where we can keep all the information related to a database like an URL, username and password, etc. for this, we have created "database.yaml" and placed it under the resources folder.

To read the yaml file we are using the snake yaml library.

Java
 
package yaml;


import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.InputStream;

public class YamlFileReader {

    public DBYamlModel getYamlData()  {
        Yaml yaml = new Yaml(new Constructor(DBYamlModel.class));
        InputStream inputStream = this.getClass()
                .getClassLoader()
                .getResourceAsStream("database.yaml");
        DBYamlModel db = yaml.load(inputStream);
        return db;
    }
}


Using the JOOQ library, we have written a few methods to fetch the data from the database.
 
public Result<Record> getQueryResult(String sqlQuery) throws SQLException {
    try (Connection c = getDBConnection()) {
        return DSL.using(c).fetch(sqlQuery);
    }
}

public <T> List<T> getQueryResultWithType(String sqlQuery, Class<T> className) throws SQLException {
   return getQueryResult(sqlQuery)
            .stream().map(e -> e.into(className)).toList();

}


Please see the full code to connect with the H2 database and fetch the data using SQL query:

Java
 
package database;

import org.jooq.Result;
import org.jooq.Record;
import org.jooq.impl.DSL;
import yaml.DBYamlModel;
import yaml.YamlFileReader;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

public class DatabaseUtil {


    private Connection getDBConnection() throws SQLException {
        YamlFileReader yamlFileReader = new YamlFileReader();
        DBYamlModel yamlData = yamlFileReader.getYamlData();
        return DriverManager.getConnection(
                yamlData.getUrl(),
                yamlData.getUsername(),
                yamlData.getPassword());

    }


    public Result<Record> getQueryResult(String sqlQuery) throws SQLException {
        try (Connection c = getDBConnection()) {
            return DSL.using(c).fetch(sqlQuery);
        }
    }

    public <T> List<T> getQueryResultWithType(String sqlQuery, Class<T> className) throws SQLException {
       return getQueryResult(sqlQuery)
                .stream().map(e -> e.into(className)).toList();

    }

}


This is a java class where we are displaying the final database output result of one query.  

Java
 
package database;

public class DBQueryTesting {

    public static void main(String[] args) throws Exception {
        String sqlQuery = "Select * from Persons";
        DatabaseUtil databaseUtil = new DatabaseUtil();
        
        databaseUtil
                .getQueryResult(sqlQuery)
                .forEach(System.out::println);
    }
}


There is a way to convert the database result response to the java class.

For example, we have database table PERSONS with 5 columns, so we have created a Person record with sample fields. Now we can map easily the database response to java code, and we can fetch column values using the java field only.

 

public record Persons(Integer Id,String LastName,String FirstName, String Address, String City) {
}


Java
 
package database;
import Data.Persons;

public class DBQueryTesting {

    public static void main(String[] args) throws Exception {
        String sqlQuery = "Select * from Persons";
        DatabaseUtil databaseUtil = new DatabaseUtil();

        databaseUtil
                .getQueryResultWithType(sqlQuery, Persons.class)
                .forEach(System.out::println);

    }

}


This is a way we can write database interaction code which we can use in our End 2 End automation where we are fetching the data from the database and performing the data verification. 

Java
 
import Data.Persons;
import database.DatabaseUtil;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.sql.SQLException;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DBCheckTest {
    DatabaseUtil databaseUtil;

    @BeforeEach
    void setUp(){
        databaseUtil = new DatabaseUtil();
    }

    @DisplayName("Test the Persons table should contains one record with Firstname as CHERRY")
    @Test
    void testPersonsFirstName() throws SQLException {
        String sqlQuery = "Select * from Persons";
        List<Persons> persons=databaseUtil.getQueryResultWithType(sqlQuery, Persons.class).stream()
                .filter(e ->e.FirstName().equals("CHERRY")).toList();

        assertEquals(1, persons.size());
        assertEquals("TOM", persons.get(0).LastName());
    }

    @DisplayName("Test the Persons table should contains two record with city as DELHI")
    @Test
    void testPersonsCityCount() throws SQLException {
        String sqlQuery = "Select * from Persons";
        List<Persons> persons=databaseUtil.getQueryResultWithType(sqlQuery, Persons.class).stream()
                .filter(e ->e.City().equals("DELHI")).toList();

        assertEquals(2, persons.size());
    }

}


Please find the GitHub repo.

This is a just small example. There are so many features that JOOQ supports.

JOOQ library offers so many methods, classes, and operation in a functional style which really help us to write code in a simple, easy and concise manner so that everyone can understand the business context easily.

Database Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • Tracing the Agentic Loop: Monitoring Multi-Round-Trip MCP Calls With OpenTelemetry
  • Retiring a Tier-0 Legacy Database Without Breaking the Business
  • The Aggregate Reference Problem

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook