网站建设公司平台,朝阳网络科技有限公司,怎么看网站谁做的,文章编辑器wordpress【MongoDB】三、使用Java连接MongoDB 实验目的实验内容练习1、开启Eclipse#xff0c;创建Java Project项目#xff0c;命名为Mongo12、添加项目依赖的jar包3、创建类MongoDemo4、连接数据库5、查看集合6、创建集合7、删除集合8、查看文档9、插入文档10、更新文档11、删除文档… 【MongoDB】三、使用Java连接MongoDB 实验目的实验内容练习1、开启Eclipse创建Java Project项目命名为Mongo12、添加项目依赖的jar包3、创建类MongoDemo4、连接数据库5、查看集合6、创建集合7、删除集合8、查看文档9、插入文档10、更新文档11、删除文档 测试新建集合course删除新建集合course在student集合中插入文档将_id为1014的学生成绩修改为80删除_id为1012的学生 实验小结 实验目的
1了解使用Java操作MongoDB的流程 2能够编写Java操作MongoDB的代码。 实验内容
练习
1、开启Eclipse创建Java Project项目命名为Mongo1 2、添加项目依赖的jar包 3、创建类MongoDemo 在类的构造函数MongoDemo()中编写代码实现对MongoDB服务器的连接。
MongoClient connection null; //存储MongoDB数据库连接对象MongoDatabase dbnull; //存储连接的数据库对象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() {MongoIterableString 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){MongoCollectionDocument collection db.getCollection(collectionname);collection.drop();} 8、查看文档 在类MongoDemo中定义findDocument ()方法主要用于查看文档。
//查看文档public void findDocument(String collectionname){MongoCollectionDocument collection db.getCollection(collectionname);FindIterableDocument 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){MongoCollectionDocument collection db.getCollection(collectionname); collection.insertOne(document);} 10、更新文档 在类MongoDemo中定义updateDocument ()方法主要用于更新文档。
//更新文档public void updateDocument(String collectionname){MongoCollectionDocument 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){MongoCollectionDocument collection db.getCollection(collectionname);collection.deleteOne(Filters.eq(_id,1012));} 测试 在类MongoDemo中定义主函数main()主要对以上定义的功能函数进行测试。在主函数中连接数据库stu在数据库stu中新建集合course删除新建集合course在student集合中插入文档{_id:“1016”name:“唐开平”sex:“女”age18major“软件技术”credits42score74}将_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){MongoCollectionDocument 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){MongoCollectionDocument 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、尽我能力帮助他人在帮助他人的同时你会深刻巩固知识。