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.