3

I'm trying to write a Java program that will connect to a MongoDB database. I already have the IP address, port, userDB, username, and password of the database I'm trying to access.

I have the mongodb-driver-3.0.0.jar file set as a referenced library in Eclipse. I created a MongoDBDriver.java file in the src folder (shown below):

import com.mongodb.MongoClient;

public class MongoDBDriver {

public static void main(String[] args) {

    MongoClient client = new MongoClient("10.66.80.152", 27017);
    String connectPoint = client.getConnectPoint();
    System.out.println(connectPoint);
    client.close();
  }

}

When I run the script, I get the following error message:

Exception in thread "main" java.lang.NoClassDefFoundError:     
com/mongodb/connection/BufferProvider
at MongoDBDriver.main(MongoDBDriver.java:7)
Caused by: java.lang.ClassNotFoundException:  
com.mongodb.connection.BufferProvider
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more

So, what am I doing wrong and how can I fix it?

MarisolFigueroa
  • 757
  • 1
  • 14
  • 31

2 Answers2

3

Exception means that you have the class path mismatch. I create small app that connect java and mongodb and do some CURD operations.

eMongoDB-Java-app I think it will be better solution to connect database.

  • Thanks! I just downloaded the app and it seems great, but I'm still not able to access the database. I added the following code to the MongoDBManager.java file: `// connect to mongodb server mongo = new MongoClient("10.66.80.152", 27017); // connect with the database mongoMgr = mongo.getDB("mbe"); boolean auth = mongoMgr.authenticate("mbeuser", "password".toCharArray()); if(auth){ System.out.println("authorized"); } else { System.out.println("not authorized"); }` – MarisolFigueroa Aug 12 '15 at 21:19
  • and I got the following error message: `com.mongodb.MongoException: not authorized for query on mbe.user` – MarisolFigueroa Aug 12 '15 at 21:22
  • Did you check 'mbeuser' user privileges? – YAS_Bangamuwage Aug 13 '15 at 03:08
0

MongoClient does not have method getConnectPoint(), class Mongo does.

You can read the official website, it will help you out.mongodb official website java tutorial

Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
Haifeng Zhang
  • 30,077
  • 19
  • 81
  • 125