Updated on 2023-11-21 GMT+08:00

Connecting to an Instance Using Java

This section describes how to use the Java to connect to a GeminiDB Cassandra instance.

Prerequisites

  • A GeminiDB Cassandra instance has been created and is running properly. For details about how to create a GeminiDB Cassandra instance, see Buying an Instance.
  • For details about how to create an ECS, see "Getting Started > Purchasing an ECS" in the Elastic Cloud Server User Guide.
  • JDK has been installed on the ECS.

Procedure

  1. Obtain the private IP address and port number of the GeminiDB Cassandra instance.

    For details about how to obtain the private IP address and port number, see Viewing the IP Address and Port Number.

  2. Log in to the ECS. For details, see "Getting Started > Logging in to an ECS" in the Elastic Cloud Server User Guide.
  3. Edit the code for connecting to the GeminiDB Cassandra instance.

    import com.datastax.driver.core.*;
    
    Cluster cluster = null;
    try {
        cluster = Cluster.builder()                                                   
                .addContactPoint("127.0.0.1")//Private IP address of the GeminiDB Cassandra instance obtained in step 1
                .withPort(8635)        //Port number of the GeminiDB Cassandra instance obtained in step 1
                .build();
        Session session = cluster.connect();                                           
    
        ResultSet rs = session.execute("select release_version from system.local");    
        Row row = rs.one();
        System.out.println(row.getString("release_version"));                          
    } finally {
        if (cluster != null) cluster.close();                                          
    }

  4. Run the sample code to check whether the result is normal.