Problem with connecting to my database from a minecraft plugin

I used this code in my DatabaseManager class:

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 MongoClientConnectionExample {
    public static void main(String[] args) {
        String connectionString = "mongodb+srv://<username>:<password>@cluster.rtgt0bj.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) {
                e.printStackTrace();
            }
        }
    }
}

and i wanna run this on the start of my plugin so i tried to use DatabaseManager.main(): in my main class onEnable but it says the “()” is wrong, can someone please help me?

Hi @Rick and welcome to MongoDB community forums!!

It appears there might be an issue with the function you are attempting to execute within the main function. The error suggests that you are calling a static function within a non-static function, which can lead to complications.

Static functions can only be called from within the class they are defined in, especially when invoked from an instance method outside that class. To resolve this, consider calling the static method from another static method.

The main methods are static and meant for executing a Java class, not for calling from other classes.

The challenge you’re facing seems to be a common programming concept, and for more in-depth explanations, I recommend exploring other open-source platforms like Stack Overflow.
Also, the documentation on Java QuickStart would be a good starting point to understand further.

If you encounter difficulties connecting to the MongoDB Atlas cluster from the driver, could you please provide the error message you’re encountering during the connection?

Best Regards,
Aasawari

ok, but where should i run the class then i need to run it on the on enable thing

I took a look at your code, and it seems like a solid attempt at connecting to MongoDB Atlas. Now, regarding the issue you’re facing when calling Database Manager.main(), the error about the “()” might be because the main method expects an array of strings as an argument (String args). Try passing an empty array when calling it, like this: java DatabaseManager.main(new String{}); Give that a shot in your onEnable method. Also, make sure you have the necessary dependencies properly set up in your project. If that doesn’t work, then you can check out https://minecraftforfreex.com/eaglercraft/. There are some good code tips there and you might find something suitable for you.

This post was flagged by the community and is temporarily hidden.