【MongoDB】三、使用Java连接MongoDB


实验目的

(1)了解使用Java操作MongoDB的流程;
(2)能够编写Java操作MongoDB的代码。


实验内容

练习

1、开启Eclipse,创建Java Project项目,命名为Mongo1

在这里插入图片描述


2、添加项目依赖的jar包

在这里插入图片描述


3、创建类MongoDemo

       在类的构造函数MongoDemo()中编写代码实现对MongoDB服务器的连接。

MongoClient connection =null;  //存储MongoDB数据库连接对象
	MongoDatabase db=null;   //存储连接的数据库对象
	public MongoDemo() {
		ServerAddress serverAddress = new ServerAddress("127.0.0.1", 27017);
		// 第一个"root" 为账号,第二个"admin"为创建账户时的数据库名称,第三个参数为密码
	     MongoCredential mongoCredential = MongoCredential.createCredential("root", "admin", "123456".toCharArray());
	    //MongoClientOptions 是连接的相关配置,类似数据库连接池的相关配置,使用默认即可
	    connection = new MongoClient(serverAddress,mongoCredential, MongoClientOptions.builder().build());             
	}

4、连接数据库

       在类MongoDemo中定义DatabaseConn ()方法,用来连接指定的数据库。

public void DatabaseConn(String dbName) {
		db = connection.getDatabase(dbName);
	}
mongdemo.DatabaseConn("stu");

5、查看集合

       在类MongoDemo中定义getCollection ()方法,主要用于查看数据库中的集合。

public void getCollection() {
		MongoIterable<String> listCollectionNames = db.listCollectionNames();// 获取db数据库中的集合列表
		for (String collectionName : listCollectionNames) {
			System.out.println(collectionName.toString());
		}
	}

6、创建集合

       在类MongoDemo中定义createCollection ()方法,主要用于创建集合。

//创建集合
	public void createCollection(String collectionname){
		db.createCollection(collectionname);
	}


7、删除集合

       在类MongoDemo中定义dropCollection()方法,主要用于删除集合。

//删除集合
	public void dropCollection(String collectionname){
		MongoCollection<Document> collection = db.getCollection(collectionname);
		collection.drop();
	}


8、查看文档

       在类MongoDemo中定义findDocument ()方法,主要用于查看文档。

//查看文档
	public void findDocument(String collectionname){
		MongoCollection<Document> collection =db.getCollection(collectionname);
		FindIterable<Document> documents = collection.find();
		System.out.println("集合"+collectionname+"中的文档有:");
		for (Document document : documents) {
			System.out.println(document);
		}
	}


9、插入文档

       在类MongoDemo中定义insertOneDocument()方法,主要用于插入单个文档。

//插入文档
	public void insertOneDocument(String collectionname,Document document){
	      MongoCollection<Document> collection = db.getCollection(collectionname);      
	      collection.insertOne(document);
	}


10、更新文档

       在类MongoDemo中定义updateDocument ()方法,主要用于更新文档。

//更新文档
	public void updateDocument(String collectionname){
	     MongoCollection<Document> collection =db.getCollection(collectionname);
	     //修改的键以及修改的值
	     Document document = new Document("score","80");
	     //用作修改
	     collection.updateOne(Filters.eq("_id","1014"),new Document("$set",document));
	}


11、删除文档

       在类MongoDemo中定义deleteDocument()方法,主要用于删除文档。

//删除文档
	public void deleteDocument(String collectionname){
		MongoCollection<Document> collection = db.getCollection(collectionname);
		collection.deleteOne(Filters.eq("_id","1012"));
	}


测试

       在类MongoDemo中定义主函数main(),主要对以上定义的功能函数进行测试。在主函数中连接数据库stu,在数据库stu中新建集合course,删除新建集合course,在student集合中插入文档{_id:“1016”,name:“唐开平”,sex:“女”,age:18,major:“软件技术”,credits:42,score:74},将_id为1014的学生成绩修改为80,删除_id为1012的学生。

新建集合course

mongdemo.createCollection("course");
mongdemo. getCollection();

删除新建集合course

mongdemo.dropCollection("course");
mongdemo.getCollection();

在student集合中插入文档

Document document = new Document("_id","1016").append("name", "唐开平").append("sex", "女").append("age","18").append("major", "软件技术").append("credits", "42").append("score", "74");
mongdemo.insertOneDocument("students",document);
mongdemo.findDocument("students");


将_id为1014的学生成绩修改为80

	//更新文档
	public void updateDocument(String collectionname){
	     MongoCollection<Document> collection =db.getCollection(collectionname);
	     //修改的键以及修改的值
	     Document document = new Document("score","80");
	     //用作修改
	     collection.updateOne(Filters.eq("_id","1014"),new Document("$set",document));
	}
mongdemo.updateDocument("students");	
mongdemo.findDocument("students");


删除_id为1012的学生

//删除文档
	public void deleteDocument(String collectionname){
		MongoCollection<Document> collection = db.getCollection(collectionname);
		collection.deleteOne(Filters.eq("_id","1012"));
	}  

mongdemo.deleteDocument("students");
mongdemo.findDocument("students");

实验小结

       通过本次实验,我掌握了通过使用Java连接MongoDB的具体流程以及使用Java对MongoDB数据库进行的增删改查等一系列操作。在实验过程中遇到了很多硬件或者是软件上的问题,请教老师,询问同学,上网查资料,都是解决这些问题的途径。最终将遇到的问题一一解决最终完成实验。
注意事项:
1、有疑问前,知识学习前,先用搜索。
2、熟读写基础知识,学得会不如学得牢。
3、选择交流平台,如QQ群,网站论坛等。
4、尽我能力帮助他人,在帮助他人的同时你会深刻巩固知识。

  • 2
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慢热型网友.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值