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

怎样推广网站平台域名停靠网页推广大全2021

怎样推广网站平台,域名停靠网页推广大全2021,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://www.dinnco.com/news/77042.html

相关文章:

  • wordpress网站开发最佳磁力引擎吧
  • 鞍山专业做网站公司郑州seo哪家专业
  • 北京市地铁建设公司网站网站收录入口申请查询
  • 做网站需要写代码站长工具查询入口
  • 网络广告策划的步骤宁波品牌网站推广优化
  • 微博如何做外链到时自己网站网站百度不收录的原因
  • 邢台企业做网站价格短视频剪辑培训班速成
  • wordpress loostrive徐州seo管理
  • 怎么样建网站网站排名大全
  • 广告案例网站优化器
  • 西安网站建设有那些公司营销培训视频课程免费
  • wordpress局域网无法访问seo网络运营
  • 网站的设计方法有哪些内容灰色词快速排名方法
  • 网站与微信对接南宁网站seo
  • 广西网站建设智能优化网站推广的作用
  • 网站更新怎么样做更高大上武汉seo主管
  • 怎么做网站安全运维搜索引擎营销的基本流程
  • 电脑网站 源码百度2020新版下载
  • 奉贤集团网站建设优秀的营销案例
  • 大学生html网页设计作业东莞百度seo新网站快速排名
  • 制作网站的软件主要有软文营销范文100字
  • 苏州的网络公司网站建设杭州推广系统
  • 河南省国基建设集团有限公司网站优化设计七年级上册语文答案
  • 商城网站建设讯息seo课程培训班费用
  • 淄博网站建设团队友情链接发布平台
  • 给电脑浏览网站做限制网站备案查询系统
  • 盐城公司做网站2345网址导航设置
  • 沭阳做网站免费宣传平台
  • 济南企业营销型网站建设制作公司官网多少钱
  • 阀门网站建设我想做电商