Hello,
I was trying to connect MongoDB with Netbeans using Java and Maven, for doing so, I followed these steps:
- Added dependencies
- 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) {
}
}
}
}