ServerAPI missing from package com.mongodb

Hello,
I was trying to connect MongoDB with Netbeans using Java and Maven, for doing so, I followed these steps:

  1. Added dependencies
  2. Pasted connection code from website

But when I pasted the code, I’m having errors in the import statement that states that the ServerAPI class and the ServerAPIVersion class are missing from the package.

Can anyone help me with this?

import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoException;
import com.mongodb.ServerApi;
import com.mongodb.ServerApiVersion;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

public class connection {
    public static void main(String[] args) {
        String connectionString = "mongodb+srv://*****:******@productmaster.weamfdx.mongodb.net/?retryWrites=true&w=majority";

        ServerApi serverApi = ServerApi.builder()
                .version(ServerApiVersion.V1)
                .build();

        MongoClientSettings settings = MongoClientSettings.builder()
                .applyConnectionString(new ConnectionString(connectionString))
                .serverApi(serverApi)
                .build();

        // Create a new client and connect to the server
        try (MongoClient mongoClient = MongoClients.create(settings)) {
            try {
                // Send a ping to confirm a successful connection
                MongoDatabase database = mongoClient.getDatabase("admin");
                database.runCommand(new Document("ping", 1));
                System.out.println("Pinged your deployment. You successfully connected to MongoDB!");
            } catch (MongoException e) {
            }
        }
    }
}

Can you share details of the artifact name on Maven as well as the Java driver version?

Hello,
Thank you for replying.
Please refer to this screenshot.

The artifact id is: mongodb-driver-sync
and the driver version mentioned is: 4.10.1

I’m seeing two driver dependencies – one on mongodb-driver (which is no longer maintained or updated by MongoDB) and one on mongodb-driver-sync (which is maintained and updated). What happens if you remove the first dependency?

So when I remove the first one, It starts giving me errors like, “getCollection” is not found, etc.