补全代码public class MongoDBJDBC{ public static void main( String args[] ){ try{ //连接到mongodb服务 MongoClient mongoClient = new MongoClient(_ (1)_ ); ; // 连接到数据库 MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol"); mongoDatabase._ (2)_ ("test"); MongoCollection collection = mongoDatabase.getCollection("test"); collection.insertOne(new Document0._ (3)_ ("name"," zhangsan")); System.out.printIn("Connect to database sucessfly"); Findlterable findlterable=collection.find(); MongoCursor mongoCursor = findlterable._ (4)_ ; while(mongoCursor._ (5)_ ){ System.out.println(mongoCursor.next(); } System.out.printIn("文档插入成功"); }catch(Exception e){ System.err.printIn( e.getClass0.getName0 + ": " + e.getMessage0 ); } } }
时间: 2024-04-01 11:33:11 浏览: 50
补全代码如下:
public class MongoDBJDBC{
public static void main( String args[] ){
try{
//连接到mongodb服务
MongoClient mongoClient = new MongoClient("localhost", 27017); ;
// 连接到数据库
MongoDatabase mongoDatabase = mongoClient.getDatabase("mycol");
mongoDatabase.createCollection("test");
MongoCollection<Document> collection = mongoDatabase.getCollection("test");
collection.insertOne(new Document("name"," zhangsan"));
System.out.println("Connect to database successfully");
FindIterable<Document> findIterable=collection.find();
MongoCursor<Document> mongoCursor = findIterable.iterator();
while(mongoCursor.hasNext()){
System.out.println(mongoCursor.next());
}
System.out.println("文档插入成功");
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
阅读全文