Connecting to your database with the DataStax Java driver

Use the DataStax Java driver to connect to your database created using the DataStax Constellation console.

Use the DataStax Java driver to connect to your Apollo database and begin building your own application.

You add a repository and dependencies to the pom.xml file for your project to download the appropriate .jar files for the DSE Java driver and make them available to your code. Additionally, you implement a ConnectDatabase class to initialize the DSE Java driver.

Tip: DataStax recommends using the DataStax Java driver for Apache Cassandra. You can also use the DataStax Enterprise (DSE) Java driver, which exposes the same API for connecting to Cassandra databases.

Prerequisites

  1. Download and install Maven.
  2. Create a DataStax Apollo database.
  3. Download the secure connect bundle to obtain connection credentials for your database.

Procedure

  1. Navigate to the pom.xml file at the root of your Java project and open it for editing.
    Tip: For a complete pom.xml file, see the Example pom.xml file.
    1. Add the dependency for your DataStax Java driver to your pom.xml file, ensuring that the name of the dependency corresponds to the installed version:

      This dependency causes Maven to automatically download the appropriate .jar files found at the url specified in the repository for the DSE Java driver and make them available to your code.

      • DataStax Java driver for Apache Cassandra 4.x

        <dependency>
          <groupId>com.datastax.oss</groupId>
          <artifactId>java-driver-core</artifactId>
          <version>4.3.0</version>
        </dependency>
        
      • DSE Java 2.x

        <dependency>
          <groupId>com.datastax.dse</groupId>
          <artifactId>dse-java-driver-core</artifactId>
          <version>2.3.0</version>
        </dependency>
        
    2. Optional: To use DSE QueryBuilder, which is a utility used to generate CQL queries programmatically, add the following dependency to your pom.xml file:
              <dependency>
                  <groupId>com.datastax.dse</groupId>
                  <artifactId>dse-java-driver-query-builder</artifactId>
                  <version>2.3.0</version>
              </dependency>
    3. Save and close your pom.xml file.
  2. Initialize the DataStax Java driver.
    1. Create a ConnectDatabase.java file in the /src/main/java directory for your Java project.
      cd javaProject/src/main/java
      touch ConnectDatabase.java
    2. Copy the following code for your DataStax driver into the ConnectDatabase.java file.

      The following example implements a ConnectDatabase class to connect to your Apollo database, runs a CQL query, and prints the output to the console.

      Note: In the DseSessionDseSession object, include the path to the secure connect bundle for your Apollo database (secure-connect-database_name.zip in the withCloudSecureConnectBundle() method as shown in the following example.

      If converting from using the open source Cassandra Java driver to the DSE Java driver, ensure that you change Session to DseSession.

      • DataStax Java driver for Apache Cassandra 4.x

        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 ConnectDatabase {
        
           public static void main(String[] args) {
               // Create the CqlSession object:
               try (CqlSession session = CqlSession.builder().withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip"))
                   .withAuthCredentials("username","password")
                   .withKeyspace("keyspace_name")
                   .build()) {
                   // Select the release_version from the system.local table:
                   ResultSet rs = session.execute("select release_version from system.local");
                   Row row = rs.one();
                   //Print the results of the CQL query to the console:
                   if (row != null) {
                       System.out.println(row.getString("release_version"));
                   } else {
                       System.out.println("An error occurred.");
                   }
               }
               System.exit(0);
           }
        }
        
      • DSE Java 2.x

        import com.datastax.dse.driver.api.core.DseSession;
        import com.datastax.oss.driver.api.core.cql.ResultSet;
        import com.datastax.oss.driver.api.core.cql.Row;
        
        public class ConnectDatabase {
        
           public static void main(String[] args) {
               // Create the DseSession object:
               try (DseSession session = DseSession.builder().withCloudSecureConnectBundle(Paths.get("/path/to/secure-connect-database_name.zip"))
                   .withAuthCredentials("username","password")
                   .withKeyspace("keyspace_name")
                   .build()) {
                   // Select the release_version from the system.local table:
                   ResultSet rs = session.execute("select release_version from system.local");
                   Row row = rs.one();
                   //Print the results of the CQL query to the console:
                   if (row != null) {
                       System.out.println(row.getString("release_version"));
                   } else {
                       System.out.println("An error occurred.");
                   }
               }
               System.exit(0);
           }
        }
    3. Save and close the ConnectDatabase.java file.

Example pom.xml file

You can use the following pom.xml file in your Java project to connect to your Apollo database. If you already have a pom.xml file for your Java project, copy only the repository and dependencies as indicated in the previous steps.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>net.techne.web</groupId>
    <artifactId>cloudTest</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- START-javaDriverDependencyCore -->
        <dependency>
            <groupId>com.datastax.dse</groupId>
            <artifactId>dse-java-driver-core</artifactId>
            <version>2.3.0</version>
        </dependency>
        <!-- END-javaDriverDependencyCore -->
        <!-- START-javaDriverDependencyQuery -->
        <dependency>
            <groupId>com.datastax.dse</groupId>
            <artifactId>dse-java-driver-query-builder</artifactId>
            <version>2.3.0</version>
        </dependency>
        <!-- END-javaDriverDependencyQuery -->
    </dependencies>
</project>

What's next

Build your application. See the DataStax Java driver for Apache Cassandra and DSE Java driver documentation for more information about using the DataStax Java driver and DSE QueryBuilder.