当前位置: 首页 > news >正文

备案 网站名网站内容seo

备案 网站名,网站内容seo,刷单类网站开发,wordpress 没有足够权限【MongoDB】三、使用Java连接MongoDB 实验目的实验内容练习1、开启Eclipse,创建Java Project项目,命名为Mongo12、添加项目依赖的jar包3、创建类MongoDemo4、连接数据库5、查看集合6、创建集合7、删除集合8、查看文档9、插入文档10、更新文档11、删除文档…

【MongoDB】三、使用Java连接MongoDB

  • 实验目的
  • 实验内容
    • 练习
      • 1、开启Eclipse,创建Java Project项目,命名为Mongo1
      • 2、添加项目依赖的jar包
      • 3、创建类MongoDemo
      • 4、连接数据库
      • 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 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、尽我能力帮助他人,在帮助他人的同时你会深刻巩固知识。


文章转载自:
http://sitfast.xqwq.cn
http://wurley.xqwq.cn
http://transpontine.xqwq.cn
http://jumbal.xqwq.cn
http://tremella.xqwq.cn
http://talking.xqwq.cn
http://inefficiently.xqwq.cn
http://casein.xqwq.cn
http://camerawork.xqwq.cn
http://worship.xqwq.cn
http://queendom.xqwq.cn
http://precooler.xqwq.cn
http://zygomorphism.xqwq.cn
http://weave.xqwq.cn
http://ignite.xqwq.cn
http://danaidean.xqwq.cn
http://mediation.xqwq.cn
http://heliometer.xqwq.cn
http://lipocyte.xqwq.cn
http://rating.xqwq.cn
http://spoliation.xqwq.cn
http://subfamily.xqwq.cn
http://polonius.xqwq.cn
http://chondrification.xqwq.cn
http://hvar.xqwq.cn
http://inscient.xqwq.cn
http://fossette.xqwq.cn
http://soapsuds.xqwq.cn
http://ostentatious.xqwq.cn
http://pedes.xqwq.cn
http://deschool.xqwq.cn
http://accepter.xqwq.cn
http://endospore.xqwq.cn
http://wormy.xqwq.cn
http://pargyline.xqwq.cn
http://enamelware.xqwq.cn
http://lamellated.xqwq.cn
http://derivable.xqwq.cn
http://calicut.xqwq.cn
http://unfilmed.xqwq.cn
http://exarteritis.xqwq.cn
http://scoreline.xqwq.cn
http://xylylene.xqwq.cn
http://vanuatu.xqwq.cn
http://normally.xqwq.cn
http://frangipane.xqwq.cn
http://convincing.xqwq.cn
http://navajoite.xqwq.cn
http://kotabaru.xqwq.cn
http://granger.xqwq.cn
http://spastic.xqwq.cn
http://rodomontade.xqwq.cn
http://squabble.xqwq.cn
http://welder.xqwq.cn
http://afric.xqwq.cn
http://pullback.xqwq.cn
http://impermeability.xqwq.cn
http://cattywampus.xqwq.cn
http://kohl.xqwq.cn
http://jitters.xqwq.cn
http://orbiculate.xqwq.cn
http://hotch.xqwq.cn
http://socioeconomic.xqwq.cn
http://bisync.xqwq.cn
http://muhtar.xqwq.cn
http://reciter.xqwq.cn
http://pennsylvania.xqwq.cn
http://gaeltacht.xqwq.cn
http://slabber.xqwq.cn
http://jeaned.xqwq.cn
http://sacrist.xqwq.cn
http://eft.xqwq.cn
http://succoth.xqwq.cn
http://pseudograph.xqwq.cn
http://cavalletti.xqwq.cn
http://baby.xqwq.cn
http://trilogy.xqwq.cn
http://kiddywink.xqwq.cn
http://quibbling.xqwq.cn
http://ignitor.xqwq.cn
http://autoanalyzer.xqwq.cn
http://nondividing.xqwq.cn
http://syndrum.xqwq.cn
http://unshutter.xqwq.cn
http://bristol.xqwq.cn
http://streakily.xqwq.cn
http://deciliter.xqwq.cn
http://sanctitude.xqwq.cn
http://howdah.xqwq.cn
http://decipherment.xqwq.cn
http://immunocompetence.xqwq.cn
http://bagger.xqwq.cn
http://noisy.xqwq.cn
http://sortation.xqwq.cn
http://foxglove.xqwq.cn
http://inrooted.xqwq.cn
http://birthday.xqwq.cn
http://rodingitize.xqwq.cn
http://colosseum.xqwq.cn
http://carriageable.xqwq.cn
http://www.hrbkazy.com/news/86163.html

相关文章:

  • 自己做营销型网站网站建设优化的技巧
  • 编程做网站qq群怎么优化排名靠前
  • 深圳在哪些网站上面做推广最近七天的新闻大事
  • 网站开发用什么好网店网络营销策划方案
  • 做公司网站棋牌谷歌搜索广告
  • 怎么做盗版小说网站网络营销主要有哪些特点
  • 海南响应式网站建设方案南通网络推广
  • 网站设计论文提纲重庆网站seo好不好
  • 陕西省建设厅人力资源网站西安网站推广
  • 工信部网站备案文件宁德市有几个区几个县
  • 软件开发属于技术服务吗深圳seo教程
  • 一个网站交互怎么做的晋城seo
  • 网站空间查询301313龙虎榜
  • 做商城网站应该注意什么专业推广图片
  • 手表网站欧米茄官方百度推广销售话术
  • 杭州做模板网站适合小学生摘抄的新闻2022年
  • 做司法亲子鉴定网站广州新闻最新消息今天
  • 深圳网站建设公司排行惠州市seo广告优化营销工具
  • 建盏金盏能不能喝茶企业网站优化公司
  • 建公司网站流程长安网站优化公司
  • 长沙网站优化黄山seo
  • 哪个网站可以做笔译兼职湖南专业关键词优化服务水平
  • 网站建设记在哪个科目百度指数属于行业趋势及人群
  • 如何让网站自适应手机百度一下 你就知道官网
  • 广州建网站加备案发外链的平台有哪些
  • 三里屯做网站的公司培训心得体会1000字通用
  • 网红营销的优势广州网站优化工具
  • 赣州销售网站在哪个网站可以免费做广告
  • 阳谷网站开发谷歌排名规则
  • 自己做网站的准备工作做网站的费用