Connect an application


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:

  • installed YugabyteDB, created a universe, and are able to interact with it using the YCQL shell. If not, follow the steps in Quick start.
  • installed JDK version 1.8 or later.
  • installed Maven 3.3 or later.

Create the project's POM

Create a file, named pom.xml, and then copy the following content into it. The Project Object Model (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.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

public class YBCqlHelloWorld {
  public static void main(String[] args) {
    try {
      // Create a Cassandra client.
      Cluster cluster = Cluster.builder()
      Session session = cluster.connect();

      // Create keyspace 'ybdemo' if it does not exist.
      String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS ybdemo;";
      ResultSet createKeyspaceResult = session.execute(createKeyspace);
      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);";
      ResultSet createResult = session.execute(createTable);
      System.out.println("Created table employee");

      // Insert a row.
      String insert = "INSERT INTO ybdemo.employee (id, name, age, language)" +
                                          " VALUES (1, 'John', 35, 'Java');";
      ResultSet insertResult = session.execute(insert);
      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());

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

You should see the following as the 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