Import java.util.ArrayList;
Import java.util.List;
Import com.mongodb.*;
public class Testmongodb {public static void main (string[] args) {
ServerAddress serverurl=new serveraddress ("Your server IP", 27017);
List<serveraddress> lists=new arraylist<serveraddress> ();
Lists.add (ServerURL);
Mongocredential credential=mongocredential.createcredential ("username", "library name", "Password". ToCharArray ());
List<mongocredential> listm=new arraylist<mongocredential> ();
Listm.add (credential);
Mongoclient client=new mongoclient (lists,listm);
List<string> Dbname=client.getdatabasenames ();
System.out.println (dbname);
}
}
Note: The library name is the library you specified when you created the user, or you will get an error saying that there is no permission
Jar Package: Download the address, you can also download the attachment directly
http://central.maven.org/maven2/org/mongodb/mongo-java-driver/