0

This is the code that I was trying to error, and gave me an error.

try {
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("mydatabase");
        DBCollection table = db.getCollection("users");

        BasicDBObject document = new BasicDBObject();
        document.put("user_id", "5");
        document.put("user_name", "David");
        table.insert(document);

    } catch (MongoException e) {
        Log.w("PPERROR", e.getClass().getName() + ": " + e.getMessage());
    } catch (UnknownHostException e) {
        Log.w("PPERROR", e.getClass().getName() + ": " + e.getMessage());
    }

I am really not sure if the database connection is actually made or not.
mongo.exe is located in "C:\mongodb\bin"
I executed mongod.exe with dbpath "C:\test\testdata"

mongod.exe --dbpath "C:\test\testdata"

And this gives me "waiting for connections on port 27017"
Lastly, the whole project is located in C:\users\david\workspace\projectName

When I run the code, I am getting an error

com.mongodb.MongoTimeoutException: Timed out while waiting for a server that matches AnyServerSelector{} after 4936 ms

after this line of code

DBCollection table = db.getCollection("users");

I suspect that mongoClient failed to get db named "mydatabase".(or not even connected to localhost) When I create mongoClient, is there anything that I can specify dbPath?

If my explanation is unclear, please let me know.

Thank you very much in advance.

EDIT
This shows the database connection if I am correct.

enter image description here

Adrian
  • 836
  • 7
  • 20
  • 44
  • Can you connect to the database via the mongo shell? – helmy Jun 15 '14 at 22:59
  • take a look at this:https://groups.google.com/forum/#!topic/sculptorgenerator/PCyggfv01sg – Saheed Hussain Jun 16 '14 at 01:48
  • @helmy I added the screenshot that shows my database connection via cmd. – Adrian Jun 16 '14 at 18:58
  • @user3266530 according to the post you linked, am I correct that I have to change configuration? – Adrian Jun 16 '14 at 18:59
  • So open another command prompt window and run the mongo shell (mongo.exe) to see if it can connect to your mongod. – helmy Jun 16 '14 at 20:28
  • Also, try turning Windows firewall (or any other firewalls) OFF. – helmy Jun 16 '14 at 20:32
  • @helmy Yes, I was able to connect to mongo.exe using cmd, but when I try to connect with java code above then it fails. – Adrian Jun 16 '14 at 21:08
  • You mentioned ADT, have you tried running this in a non-ADT project? e.g. create a plain Java project and a simple class with main() and the code above. – helmy Jun 17 '14 at 01:51
  • Any solution? same problem here; by using an older java library (2.5) works.. do you also have an old version of mongodb running? – ianaz Aug 28 '14 at 15:19

0 Answers0