Skip to main content
This tutorial shows you how build a simple Java application with CockroachDB and jOOQ. CockroachDB is supported in jOOQ Professional and Enterprise editions.
We recommend using Java versions 8+ with CockroachDB.
For another use of jOOQ with CockroachDB, see our examples-orms repository.

Before you begin

  1. .
  2. Start up a or local cluster.
  3. Choose the instructions that correspond to whether your cluster is secure or insecure:
The --insecure flag used in this tutorial is intended for non-production testing only. To run CockroachDB in production, use a secure cluster instead.

Step 1. Install Maven

This tutorial uses the Maven build tool to manage application dependencies. To install Maven on Mac, run the following command:
To install Maven on a Debian-based Linux distribution like Ubuntu:
For other ways to install Maven, see its official documentation.

Step 2. Install jOOQ

Download the free trial of jOOQ Professional or Enterprise edition from jOOQ’s website, and unzip the file. To install jOOQ to your machine’s local Maven repository, run the maven-install.sh script included in the jOOQ install folder:

Step 3. Create the maxroach user and bank database

Start the :
In the SQL shell, issue the following statements to create the maxroach user and bank database:
Give the maxroach user the necessary permissions:
Exit the SQL shell:

Step 4. Generate a certificate for the maxroach user

Create a certificate and key for the maxroach user by running the following command. The code samples will run as this user. The generates a key in PKCS#8 format, which is the standard key encoding format in Java. In this case, the generated PKCS8 key will be named client.maxroach.key.pk8.

Step 5. Run the Java code

The code below uses jOOQ to map Java methods to SQL operations. It performs the following steps, some of which correspond to method calls of the Sample class.
  1. Inputs the db.sql file to the database. db.sql includes SQL statements that create an accounts table in the bank database.
  2. Inserts rows into the accounts table using session.save(new Account(int id, int balance)) (see Sample.addAccounts()).
  3. Transfers money from one account to another, printing out account balances before and after the transfer (see transferFunds(long fromId, long toId, long amount)).
  4. Prints out account balances before and after the transfer (see Sample.getAccountBalance(long id)).
In addition, the code shows a pattern for automatically handling by wrapping transactions in a higher-order function Sample.runTransaction(). It also includes a method for testing the retry handling logic (Sample.forceRetryLogic()), which will be run if you set the FORCE_RETRY variable to true. To run it:
  1. Download and unzip jooq-basic-sample.zip.
  2. Open jooq-basic-sample/src/main/java/com/cockroachlabs/Sample.java, and edit the connection string passed to DriverManager.getConnection() in the Sample class’s main() method so that the certificate paths are fully and correctly specified.
  3. Compile and run the code using Maven:
Here are the contents of Sample.java, the Java file containing the main Sample class:
Toward the end of the output, you should see:
To verify that the account balances were updated successfully, start the :
To check the account balances, issue the following statement:

Step 3. Create the maxroach user and bank database

Start the :
In the SQL shell, issue the following statements to create the maxroach user and bank database:
Give the maxroach user the necessary permissions:
Exit the SQL shell:

Step 4. Run the Java code

The code below uses jOOQ to map Java methods to SQL operations. It performs the following steps, some of which correspond to method calls of the Sample class.
  1. Inputs the db.sql file to the database. db.sql includes SQL statements that create an accounts table in the bank database.
  2. Inserts rows into the accounts table using session.save(new Account(int id, int balance)) (see Sample.addAccounts()).
  3. Transfers money from one account to another, printing out account balances before and after the transfer (see transferFunds(long fromId, long toId, long amount)).
  4. Prints out account balances before and after the transfer (see Sample.getAccountBalance(long id)).
In addition, the code shows a pattern for automatically handling by wrapping transactions in a higher-order function Sample.runTransaction(). It also includes a method for testing the retry handling logic (Sample.forceRetryLogic()), which will be run if you set the FORCE_RETRY variable to true. To run it:
  1. Download and unzip jooq-basic-sample.zip.
  2. Compile and run the code using Maven:
Here are the contents of Sample.java, the Java file containing the main Sample class:
Toward the end of the output, you should see:
To verify that the account balances were updated successfully, start the :
To check the account balances, issue the following statement:

What’s next?

Read more about using jOOQ, or check out a more realistic implementation of jOOQ with CockroachDB in our examples-orms repository. You might also be interested in the following pages: