Get a database object (Java)

When using a client, most Data API interactions require you to connect to a database through a DataAPIClient object.

Connecting to a database is an essential component of most Data API client scripts. For more information, see Get started with the Data API (Java).

For more information about the Data API client hierarchy, see Python client internals, TypeScript client internals, Go client internals, Java client internals, and C# client internals.

Result

Returns a Database object that you can use to perform CRUD operations on your database

Parameters

Use the getDatabase method, which belongs to the com.datastax.astra.client.DataAPIClient class.

Method signature
public Database getDatabase(String apiEndpoint, DatabaseOptions dbOptions)
public Database getDatabase(String apiEndpoint, String keyspace)
public Database getDatabase(String apiEndpoint)
public Database getDatabase(UUID databaseId)
public Database getDatabase(UUID databaseId, DatabaseOptions dbOptions)
public Database getDatabase(UUID databaseId, String region, DatabaseOptions dbOptions)
Name Type Summary

apiEndpoint

String

A database API endpoint URL, such as https://DATABASE_ID-REGION.apps.astra.datastax.com.

databaseId

UUID

A database ID. You can optionally specify a region for multi-region databases.

DataStax recommends using apiEndpoint instead.

keyspace

String

Optional. Specify the working keyspace. This keyspace is used for all downstream operations unless you later override it with a method such as useKeyspace, getCollection, createCollection, getTable, or createTable.

Default: default_keyspace

region

String

The region to use for connecting to the database.

The database must be deployed in that region.

If you don’t set this parameter and the region cannot be inferred from an API endpoint, an additional request is made to determine the default region and use it in subsequent operations.

dbOptions

DatabaseOptions

The options to use for the database.

Examples

The following examples demonstrate how to connect to a database.

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.options.DataAPIClientOptions;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.databases.DatabaseOptions;

public class Example {
  public static void main(String[] args) {
    DataAPIClient client = new DataAPIClient(new DataAPIClientOptions());

    Database database =
        client.getDatabase(
            "API_ENDPOINT",
            new DatabaseOptions("APPLICATION_TOKEN", new DataAPIClientOptions()));
  }
}

Client reference

For more information, see the client reference.

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