Project: YugabyteDB (App Definition and Development / Database)
File: ycql-4.6.md

title: Connect an application linkTitle: Connect an app description: Java driver for YCQL menu: v2.16: identifier: ycql-java-driver-4.6 parent: java-drivers weight: 500 type: docs <ul class="nav nav-tabs-alt nav-tabs-yb"> <li> <a href="../yugabyte-jdbc/" class="nav-link"> YSQL </a> </li> <li class="active"> <a href="../ycql/" class="nav-link"> YCQL </a> </li> </ul> <ul class="nav nav-tabs-alt nav-tabs-yb"> <li > <a href="../ycql/" class="nav-link"> <i class="icon-cassandra" aria-hidden="true"></i> YugabyteDB Java Driver for YCQL (3.10) </a> </li> <li > <a href="../ycql-4.6/" class="nav-link active"> <i class="icon-cassandra" aria-hidden="true"></i> YugabyteDB Java Driver for YCQL (4.6) </a> </li> </ul> To build a sample Java application with the , add the following Maven dependency to your application: ```xml <dependency> <groupId>com.yugabyte</groupId> <artifactId>java-driver-core</artifactId> <version>4.6.0-yb-6</version> </dependency> ``` 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 . installed JDK version 1.8 or later. installed Maven 3.3 or later. 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 <?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <modelVersion>4.0.0</modelVersion> <groupId>com.yugabyte.sample.apps</groupId> <artifactId>hello-world</artifactId> <version>1.0</version> <packaging>jar</packaging> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>com.yugabyte</groupId> <artifactId>java-driver-core</artifactId> <version>4.6.0-yb-6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>2.1</version> <executions> <execution> <id>copy-dependencies</id> <phase>prepare-package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> <overWriteReleases>true</overWriteReleases> <overWriteSnapshots>true</overWriteSnapshots> <overWriteIfNewer>true</overWriteIfNewer> </configuration> </execution> </executions> </plugin> </plugins> </build> </project> ``` Create the appropriate directory structure as expected by Maven. ```sh $ mkdir -p src/main/java/com/yugabyte/sample/apps ``` Copy the following contents into the file `src/main/java/com/yugabyte/sample/apps/YBCqlHelloWorld.java`. ```java package com.yugabyte.sample.apps; import java.net.InetSocketAddress; 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 { public static void main(String[] args) { try { // Create a YCQL client. CqlSession session = CqlSession .builder() .addContactPoint(new InetSocketAddress("127.0.0.1", 9042)) .withLocalDatacenter("datacenter1") .build(); // Create keyspace 'ybdemo' if it does not exist. String createKeyspace = "CREATE KEYSPACE IF NOT EXISTS ybdemo;"; 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);"; 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');"; 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. session.close(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } } } ``` To build the project, run the following `mvn package` command. ```sh $ mvn package ``` You should see a `BUILD SUCCESS` message. To use the application, run the following command. ```sh $ java -cp "target/hello-world-1.0.jar:target/lib/*" com.yugabyte.sample.apps.YBCqlHelloWorld ``` You should see the following as the output. ```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 ```