The following instructions show how you can build a Java application connected to YugabyteDB Managed using the Yugabyte Java Driver for YCQL v4.6.


To build a sample Java application with the Yugabyte Java Driver for YCQL, add the following Maven dependency to your application:


Create the sample Java application


This tutorial assumes that you have the following:

  • A YugabyteDB Managed cluster, with your database credentials (username and password)
  • JDK version 1.8 or later
  • Maven 3.3 or later

Add your computer to the cluster IP allow list. Refer to Assign IP allow lists.

You also need to download and install your YugabyteDB Managed cluster CA certificate and obtain the cluster connection parameters as follows:

  1. Sign in to YugabyteDB Managed, select your cluster, and click Connect.

  2. Click Connect to your Application.

  3. Click Download CA Cert to download the cluster root.crt certificate to your computer.

  4. Click YCQL to display the connection parameters. These include:

    • LocalDatacenter - The name of the local data center for the cluster.
    • Host - The cluster host name.
    • Port - The port number of the YCQL client API on the YugabyteDB database (9042).

Create the project's POM

Create a Project Object Model (POM) file, named pom.xml, and then copy the following content into it. The POM includes configuration information required to build the project.

<?xml version="1.0"?>




Write a sample Java application

Create the appropriate directory structure as expected by Maven.

$ mkdir -p src/main/java/com/yugabyte/sample/apps

Copy the following contents into the file src/main/java/com/yugabyte/sample/apps/

package com.yugabyte.sample.apps;
import java.util.List;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;

public class YBCqlHelloWorld {
    // Load the cluster root certificate
    private static SSLContext createSSLHandler(String certfile) {
        try {
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            FileInputStream fis = new FileInputStream(certfile);
            X509Certificate ca;
            try {
              ca = (X509Certificate) cf.generateCertificate(fis);
            } catch (Exception e) {
              System.err.println("Exception generating certificate from input file: " + e);
              return null;
            } finally {

            // Create a KeyStore containing our trusted CAs
            String keyStoreType = KeyStore.getDefaultType();
            KeyStore keyStore = KeyStore.getInstance(keyStoreType);
            keyStore.load(null, null);
            keyStore.setCertificateEntry("ca", ca);

            // Create a TrustManager that trusts the CAs in our KeyStore
            String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
            TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);

            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, tmf.getTrustManagers(), null);
            return sslContext;
        } catch (Exception e) {
            System.err.println("Exception creating sslContext: " + e);
            return null;

    public static void main(String[] args) {
        try {
            if (args.length != 4) {
                System.out.println("Usage YBCqlHelloWorld " +
                    "<ip-address> <ssl_cert_path> <username> <password>");

            // Create a YCQL client.
            CqlSession session = CqlSession
                .addContactPoint(new InetSocketAddress(args[0], 9042))
                .withAuthCredentials(args[2], args[3])
            // Create keyspace 'ybdemo' if it does not exist.
            String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS ybdemo;";
            System.out.println("Created keyspace ybdemo");
            // Create table 'employee', if it does not exist.
            String createTable = "CREATE TABLE IF NOT EXISTS ybdemo.employee (id int PRIMARY KEY, " +
                "name varchar, " + "age int, " + "language varchar);";
            System.out.println("Created table employee");
            // Insert a row.
            String insert = "INSERT INTO ybdemo.employee (id, name, age, language)" +
                " VALUES (1, 'John', 35, 'Java');";
            System.out.println("Inserted data: " + insert);
            // Query the row and print out the result.
            String select = "SELECT name, age, language FROM ybdemo.employee WHERE id = 1;";
            ResultSet selectResult = session.execute(select);
            List < Row > rows = selectResult.all();
            String name = rows.get(0).getString(0);
            int age = rows.get(0).getInt(1);
            String language = rows.get(0).getString(2);
            System.out.println("Query returned " + rows.size() + " row: " + "name=" + name +
                ", age=" + age + ", language: " + language);
            // Close the client.
        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());

Edit the .withLocalDatacenter line by replacing "datacenter1" with the LocalDatacenter from your cluster connection parameters.

You can also find the local data center name by running the following YCQL query from YugabyteDB Managed Shell:

admin@ycqlsh:yugabyte> SELECT * FROM system.local;

Add the root certificate to the project

To add the root certificate you downloaded to the application project, create a resources directory in your Java project.

$ mkdir -p src/main/resources

Then copy the root certificate root.crt into the src/main/resources directory.

Build the project

To build the project, run the following mvn package command.

$ mvn package

You should see a BUILD SUCCESS message.

Run the application

To use the application, run the following command.

$ java -cp "target/hello-world-1.0.jar:target/lib/*" \
    com.yugabyte.sample.apps.YBCqlHelloWorld \

Replace the following command line variables with the appropriate connection parameters and database credentials:

Variable Description
YUGABYTE_CLOUD_HOSTNAME The hostname of your YugabyteDB Managed cluster
ROOT_CERT_PATH The path to root.crt
YCQL_USER Your Yugabyte database username
YCQL_PASSWORD Your Yugabyte database password

For example:

$ java -cp "target/hello-world-1.0.jar:target/lib/*" \
    com.yugabyte.sample.apps.YBCqlHelloWorld \ \
    src/main/resources/root.crt admin qwerty

You should see the following output:

Created keyspace ybdemo
Created table employee
Inserted data: INSERT INTO ybdemo.employee (id, name, age, language) VALUES (1, 'John', 35, 'Java');
Query returned 1 row: name=John, age=35, language: Java