• 实现java连接mongoDB(简单封装)


      最近要完成一个登陆注册的功能,加上刚刚学了MongoDB,就想试一试,查了很多博客,都不能找到一篇完整的用例,只有自己折腾下了。这里我们需要加载一个jar包,我用的是mongo-java-driver-3.8.0.jar,主要用到的是client下的相关组件。

    
    
    package com.amyyang.mongodb;
    
    import com.mongodb.MongoClient;
    import com.mongodb.client.FindIterable;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoCursor;
    import com.mongodb.client.MongoDatabase;
    import com.mongodb.client.model.Filters;
    
    import org.bson.Document;
    import org.bson.conversions.Bson;
    
    public class MongodbHelper {
    	/* 建立连接 */
    	public static MongoDatabase setConnection() {
    		try {
    			// 	连接MongoDB服务
    			@SuppressWarnings("resource")
    			MongoClient mongoClient=new MongoClient("localhost",27017);
    			// 连接到数据库
    			MongoDatabase mongoDatabase=mongoClient.getDatabase("test");
    			System.out.println("successfully connecting"+mongoClient);
    			return mongoDatabase;
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}
    		return null;
    	}
    	/* 默认collection */
    	public static String userinfoCollection() {
    		return "info";
    	}
    	@SuppressWarnings({ })
    	/* 查找JSON */
    	public static boolean checkinfo(String user,String password) {
    		boolean result=false;
    		if(setConnection()!=null) {
    			MongoDatabase mongoDatabase=setConnection();
    			MongoCollection<Document> collection=mongoDatabase.getCollection(userinfoCollection());
    			// "userid","userpassword"是我info集合下的字段名
    			Bson filter=Filters.eq("userid",user);
    			FindIterable<Document> findIterable=collection.find(filter);
    			MongoCursor<Document> cursor=findIterable.iterator();
    			while(cursor.hasNext()) {
    				Bson refilter=Filters.eq("userpasswd",password);
    				FindIterable<Document> refindIterable=collection.find(refilter);
    				MongoCursor<Document> recursor=refindIterable.iterator();
    				while(recursor.hasNext()) {
    					System.out.println(recursor.next());
    					result=true;
    					return true;
    				}
    			}
    		}else {
    			// 连接MongeDB失败
    			System.out.println("Connection failure");
    			return false;
    		}
    		return result;
    	}
    	/* 插入JSON */
    	public static boolean insertinfo(String user,String password) {
    		if(setConnection()!=null) {
    			MongoDatabase mongoDatabase=setConnection();
    			// System.out.println(mongoDatabase);
    			MongoCollection<Document> collection=mongoDatabase.getCollection(userinfoCollection());
    			// System.out.println(userinfoCollection());
    			Document document=new Document("userid",user).append("userpasswd", password);
    			collection.insertOne(document);
    			return true;
    		}else {
    			// 连接MongeDB失败
    			System.out.println("Connection failure");
    			return false;
    		}	
    	}
    	/* 更新JSON */
    	public static boolean updateinfo(String user,String newpasswd) {
    		if(setConnection()!=null) {
    			MongoDatabase mongoDatabase=setConnection();
    			MongoCollection<Document> collection=mongoDatabase.getCollection(userinfoCollection());
    			Bson filter=Filters.eq("userid",user);
    			Document document=new Document("$set",new Document("userpasswd",newpasswd));
    			collection.updateOne(filter, document);
    			return true;
    		}else {
    			// 连接MongeDB失败
    			System.out.println("Connection failure");
    			return false;
    		}
    	}
    	// 进行简单测试
    	public static void main(String[] args) {
    		setConnection();
    		boolean rs1=checkinfo("admin", "passwd1");
    		System.out.print(rs1);
    		boolean rs2=insertinfo("newadmin", "newpasswd");
    		System.out.print(rs2);
    		boolean rs3=updateinfo("admin", "passwd");
    		System.out.print(rs3);
    	}
    }
    
    
    

      

     

      到这我简单的对mongodb的操作进行了封装,方便后面servlet的请求验证。2020马上要来了,祝大家新年快乐,万事如意!

  • 相关阅读:
    Android自定义之仿360Root大师水纹效果
    Android之TextView的Span样式源码剖析
    Android之TextView的样式类Span的使用详解
    随着ScrollView的滑动,渐渐的执行动画View
    仿微信主界面导航栏图标字体颜色的变化
    android自定义之 5.0 风格progressBar
    Android性能优化之内存篇
    Android性能优化之运算篇
    How to install Zabbix5.0 LTS version with Yum on the CentOS 7.8 system?
    How to install Zabbix4.0 LTS version with Yum on the Oracle Linux 7.3 system?
  • 原文地址:https://www.cnblogs.com/SHNLQS-1273196803/p/12200784.html
Copyright © 2020-2023  润新知