无法在java eclipse中创建MongoClient(uri)

时间:2014-03-25 23:01:39

标签: java eclipse mongodb

我正在尝试连接到mongolab主机服务器。定义了数据库集合,我可以通过shell添加和查看条目。

但是我似乎无法查看eclipse中的条目。我在线程" main"中得到一个例外。错误未解决的编译问题。编辑器在新的MongoClient(uri)行显示 - 未处理的异常类型UnknownHostException错误。我该如何解决这个问题?

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);

1 个答案:

答案 0 :(得分:0)

试试这个 -

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();                
}

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(); }