Java连接mongodb报错了,怎么解决
1个回答
2016-04-30 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
关注
展开全部
package com.fz.test;
import java.net.UnknownHostException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;
public class SimpleTest {
public static void main(String[] args) throws UnknownHostException,
MongoException {
Mongo mg = new Mongo("192.168.2.77", 27017);
// 查询所有的Database
for (String name : mg.getDatabaseNames()) {
System.out.println("dbName: " + name);
}
DB db = mg.getDB("test");
// 查询所有的聚集集合
for (String name : db.getCollectionNames()) {
System.out.println("collectionName: " + name);
}
DBCollection users = db.getCollection("users");
// 查询所有的数据
DBCursor cur = users.find();
while (cur.hasNext()) {
System.out.println(cur.next());
}
System.out.println(cur.count());
System.out.println(cur.getCursorId());
System.out.println(JSON.serialize(cur));
}
}
import java.net.UnknownHostException;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.util.JSON;
public class SimpleTest {
public static void main(String[] args) throws UnknownHostException,
MongoException {
Mongo mg = new Mongo("192.168.2.77", 27017);
// 查询所有的Database
for (String name : mg.getDatabaseNames()) {
System.out.println("dbName: " + name);
}
DB db = mg.getDB("test");
// 查询所有的聚集集合
for (String name : db.getCollectionNames()) {
System.out.println("collectionName: " + name);
}
DBCollection users = db.getCollection("users");
// 查询所有的数据
DBCursor cur = users.find();
while (cur.hasNext()) {
System.out.println(cur.next());
}
System.out.println(cur.count());
System.out.println(cur.getCursorId());
System.out.println(JSON.serialize(cur));
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询