/**
* 测试MongoDB连接
*/
private static void testMongoDBConn() {
MongoClient client = null;
try {
// 用户名 数据库 密码
MongoCredential credential = MongoCredential.createCredential("guoxin01", "guoxin", "123456".toCharArray());
//IP port
ServerAddress addr = new ServerAddress("192.168.1.137", 27017);
client = new MongoClient(addr,Arrays.asList(credential));
//得到数据库
MongoDatabase mdb = client.getDatabase("guoxin");
//得到Table
MongoCollection<?> table = mdb.getCollection("blog");
//查询所有
FindIterable<?> fi = table.find();
//遍历结果
for (Object o : fi) {
System.out.println(o);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (client != null) {
client.close();
}
}
}