0

I'm trying to connect to a mongolab host server. The database collection is defined and I can add and view entries trough the shell.

However I cant seem to get to view the entries in eclipse. I get an exception in thread "main" error unresolved compilation problem. The editor shows at the new MongoClient(uri) line - Unhandled exception type UnknownHostException error. How do I fix this?

String mongolabUri = "mongodb://username:password@dsDATA.mongolab.com:DATA/database";
MongoClientURI uri = new MongoClientURI(mongolabUri);
MongoClient client = new MongoClient(uri);
DB database = client.getDB("database");
DBCollection collection = database.getCollection("testcollections");
DBObject document = collection.findOne();
System.out.println(document);
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
Gerrie van Wyk
  • 679
  • 8
  • 27

1 Answers1

0

Try this -

String mongolabUri = "mongodb://username:password@dsDATA.mongolab.com:DATA/database";
MongoClientURI uri = new MongoClientURI(mongolabUri);
MongoClient client = null;
try{
    client = new MongoClient(uri);
    DB database = client.getDB("database");
    DBCollection collection = database.getCollection("testcollections");
    DBObject document = collection.findOne();
    System.out.println(document);
}catch(UnknownHostException e){
    e.printStackTrace();                
}

hellboy
  • 2,222
  • 1
  • 14
  • 19