We recommend using Java versions 8+ with CockroachDB.
Before you begin
- .
- Start up a or local cluster.
- Choose the instructions that correspond to whether your cluster is secure or insecure:
Step 1. Install Maven
This tutorial uses the Maven build tool to manage application dependencies. To install Maven on Mac, run the following command: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 themaven-install.sh script included in the jOOQ install folder:
Step 3. Create the maxroach user and bank database
Start the :
maxroach user and bank database:
maxroach user the necessary permissions:
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 theSample class.
- Inputs the
db.sqlfile to the database.db.sqlincludes SQL statements that create anaccountstable in thebankdatabase. - Inserts rows into the
accountstable usingsession.save(new Account(int id, int balance))(seeSample.addAccounts()). - Transfers money from one account to another, printing out account balances before and after the transfer (see
transferFunds(long fromId, long toId, long amount)). - Prints out account balances before and after the transfer (see
Sample.getAccountBalance(long id)).
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:
- Download and unzip jooq-basic-sample.zip.
-
Open
jooq-basic-sample/src/main/java/com/cockroachlabs/Sample.java, and edit the connection string passed toDriverManager.getConnection()in theSampleclass’smain()method so that the certificate paths are fully and correctly specified. -
Compile and run the code using Maven:
Sample.java, the Java file containing the main Sample class:
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: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 themaven-install.sh script included in the jOOQ install folder:
Step 3. Create the maxroach user and bank database
Start the :
maxroach user and bank database:
maxroach user the necessary permissions:
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 theSample class.
- Inputs the
db.sqlfile to the database.db.sqlincludes SQL statements that create anaccountstable in thebankdatabase. - Inserts rows into the
accountstable usingsession.save(new Account(int id, int balance))(seeSample.addAccounts()). - Transfers money from one account to another, printing out account balances before and after the transfer (see
transferFunds(long fromId, long toId, long amount)). - Prints out account balances before and after the transfer (see
Sample.getAccountBalance(long id)).
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:
- Download and unzip jooq-basic-sample.zip.
-
Compile and run the code using Maven:
Sample.java, the Java file containing the main Sample class:
What’s next?
Read more about using jOOQ, or check out a more realistic implementation of jOOQ with CockroachDB in ourexamples-orms repository.
You might also be interested in the following pages:

