Connect with the Java driver

Because Astra DB is based on Apache Cassandra®, you can use Cassandra drivers to connect to your Astra Managed Clusters databases.

To use the Java driver, you need to add the driver dependency to your pom.xml, use the ConnectDatabase class to initialize the driver, and then connect the driver to your database. Once connected, your scripts can use the driver to run commands against your database.

This quickstart explains how to use the Java driver to connect to an Astra Managed Clusters database and send some Cassandra Query Language (CQL) statements to the database. It also explains how to upgrade from an earlier version of the Java driver to a version that supports Astra DB.

Prerequisites

  1. Install Apache Maven™.

  2. Install a current Java version.

  3. Create a database.

  4. Download your database’s Secure Connect Bundle (SCB).

    For multi-region databases, download the SCB for a region that is geographically close to your application to reduce latency.

    If you need to connect to multiple regions in the same application, you need the SCB for each region, and your driver code must instantiate one root object (session) for each region. For more information, see Best practices for Cassandra drivers.

  5. Set the following environment variables:

Driver authentication methods

There are two driver authentication methods:

token authentication

The token authentication method is supported and recommended for most recent driver versions.

In your driver authentication code, pass the literal string token as the username and your application token value (AstraCS:…​) as the password. For example:

("token", "AstraCS:...")
clientId and secret authentication

If you are on an older driver version that doesn’t support token authentication, then you might need to use clientId and secret.

When you generate an application token, download or copy the token.json that contains the following values:

{
  "clientId": "CLIENT_ID",
  "secret": "CLIENT_SECRET",
  "token": "APPLICATION_TOKEN"
}

Then, in your driver authentication code, pass clientId as the username and secret as the password. For example:

("CLIENT_ID", "SECRET")

For more information, see Token details.

Add the Java driver dependency

  1. In your project’s pom.xml file, add a dependency for the Apache Cassandra Java driver Latest cassandra-java-driver release on GitHub.

    If you install an earlier version of the driver, make sure your version is compatible with Astra DB. If you need to query vector data in Astra DB Serverless (vector) databases, make sure your version also supports vector data. For more information, see Cassandra drivers supported by DataStax.

    The groupId depends on the driver version:

    Version 4.18 and later
    <dependency>
      <groupId>org.apache.cassandra</groupId>
      <artifactId>java-driver-core</artifactId>
      <version>VERSION</version>
    </dependency>
    Version 4.17 and earlier
    <dependency>
      <groupId>com.datastax.oss</groupId>
      <artifactId>java-driver-core</artifactId>
      <version>VERSION</version>
    </dependency>

Initialize and connect the Java driver

  1. In your Java project, navigate to /src/main/java, and then create a ConnectDatabase.java file:

    cd JAVA_PROJECT_DIRECTORY/src/main/java
    touch ConnectDatabase.java
  2. Copy the following code into ConnectDatabase.java, and then replace PATH/TO/SCB.zip with the absolute path to your database’s Secure Connect Bundle (SCB) zip file (secure-connect-DATABASE_NAME.zip):

ConnectDatabase.java
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;
import java.nio.file.Paths;

public class ConnectDatabase {

   public static void main(String[] args) {
       // Create the CqlSession object:
       try (CqlSession session = CqlSession.builder()
           .withCloudSecureConnectBundle(Paths.get("PATH/TO/SCB.zip"))
           .withAuthCredentials("token", System.getenv("APPLICATION_TOKEN"))
           .withKeyspace(System.getenv("ASTRA_DB_KEYSPACE"))
           .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);
   }
}

+ This code imports dependencies, initializes the Java driver, implements the ConnectDatabase class to connect to your Astra Managed Clusters database, runs a CQL query, and then prints the output to the console.

+ The example SELECT statement runs against the system.local table. You can replace this statement with any CQL statement on any keyspace or table in your database. Edit the KEYSPACE_NAME environment variable, if necessary.

  1. Save ConnectDatabase.java, and then build your Maven project.

    If you ran the example SELECT statement on the system.local table, then the cluster_name value from the system.local table is printed to the console if the script runs successfully.

Upgrade the Java driver

Use these steps if you need to upgrade from an earlier version of the Java driver to a version that supports Astra DB:

  1. Complete the prerequisites.

  2. In your pom.xml, update the dependency for the Apache Cassandra Java driver Latest cassandra-java-driver release on GitHub.

    If you are upgrading from a version earlier than 4.18.0, make sure that you update the groupId as well as the version:

    <dependency>
      <groupId>org.apache.cassandra</groupId>
      <artifactId>java-driver-core</artifactId>
      <version>VERSION</version>
    </dependency>
  3. In your existing Java driver code, modify the connection code to use the SCB and token authentication:

            CqlSessionBuilder builder = CqlSession.builder();
            builder.withCloudSecureConnectBundle(Paths.get("PATH/TO/SCB.zip"));
            builder.withAuthCredentials("token", System.getenv("APPLICATION_TOKEN"));

    For more information, see Initialize and connect the Java driver.

  4. Build your project.

Next steps

You can extend or modify the example script used in this guide to run other commands against your database, or connect to other databases. For more information, see the following:

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM