Document Database Service

Java Example

2025-07-14 07:16:26

You can connect instances using Java without downloading the SSL certificate or by downloading the SSL certificate. The connection using the SSL certificate is encrypted and features higher security. The SSL data encryption is disabled by default for DDS instances. The SSL connection enables data encryption, but it also increases network connection response time and CPU usage.

  • Parameter description.

Parameter

Description

username

Current username.

password

Password of the current user.

instance_ip

If an elastic cloud server is used for connection, "instance_ip" is the IP address of the server, that is, the Intranet address of the instance on the Basic Information page. If the instance is accessed through a device connected to the Internet, "instance_ip" is the elastic IP bound to the instance. If you need to configure a highly available address, that is, Database Connection of the instance on the Basic Information page.

instance_port

The port number is 8030 by default and 9030 for clusters. Current port number. Refer to the Database Port of the instance on the Basic Information page.

database_name

Database name, that is, the name of the database you want to connect to.

authSource

Authenticated user database. The value is admin.

ssl

Connection mode. When the value is true, the SSL connection mode is enabled.

  • Maven configuration.

<dependencies>
    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.12.10</version>
    </dependency>
</dependencies>
  • Connection without SSL certificate.

public class Connector {
    public static void main(String[] args) {
        try {
            ConnectionString connString = new ConnectionString("mongodb://<username>:<password>@<instance_ip>:<instance_port>/<database_name>?authSource=admin");
            MongoClientSettings settings = MongoClientSettings.builder()
                    .applyConnectionString(connString)
                    .retryWrites(true)
                    .build();
            MongoClient mongoClient = MongoClients.create(settings);
            MongoDatabase database = mongoClient.getDatabase("admin");  
            BsonDocument command = new BsonDocument("ping", new BsonInt64(1));
            Document commandResult = database.runCommand(command);
            System.out.println("Connect to database successfully");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Connect failed");
        }
    }
}
  • Connection using SSL certificate.

Use keytool to configure the CA certificate:

keytool -importcert -trustcacerts -file <path to certificate authority file> -keystore <path to trust store> -storepass <password>

Set JVM system properties in your program to point to the correct trust and key stores:

System.setProperty("javax.net.ssl.trustStore","<path to trust store>");
System.setProperty("javax.net.ssl.trustStorePassword","<password>");

Java Code:

public class Connector {
    public static void main(String[] args) {
        try {
            System.setProperty("javax.net.ssl.trustStore", "./conf/certs.keystore");
            System.setProperty("javax.net.ssl.trustStorePassword", "password");
            ConnectionString connString = new ConnectionString("mongodb://<username>:<password>@<instance_ip>:<instance_port>/<database_name>?authSource=admin&ssl=true");
            MongoClientSettings settings = MongoClientSettings.builder()
                    .applyConnectionString(connString)
                    .applyToSslSettings(builder -> builder.enabled(true))
                    .applyToSslSettings(builder -> builder.invalidHostNameAllowed(true))
                    .build();
            MongoClient mongoClient = MongoClients.create(settings);
            MongoDatabase database = mongoClient.getDatabase("admin");
            BsonDocument command = new BsonDocument("ping", new BsonInt64(1));
            Document commandResult = database.runCommand(command);
            System.out.println("Connect to database successfully");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Connect failed");
        }
    }
}


wkibY4HLA8wS