zoukankan      html  css  js  c++  java
  • java 使用mongodb

    1.先连接你的mongodb

    看连接是否有问题,代码

    public class MongoDB2 {
    
    	private static MongoDatabase mongoDatabase = null;
    	private static int port = 27017;
    	private static String userName = "XX";
    	private static String password="XX" ;
    	private static String database = "gatp";
    	private static String host="XXX";
    	
    	
    	/**
    	 * mongo db 连接
    	 * 
    	 * 
    	 */
    	public  void mongoConnect() {
    		try {
    			// host和port进行转换
    			encryptionDecryption decryption = new encryptionDecryption();
    			ServerAddress serverAddress = new ServerAddress(host, port);
    			List<ServerAddress> addresses = new ArrayList<ServerAddress>();
    			addresses.add(serverAddress);
    			MongoCredential credential = MongoCredential.createCredential(userName,database, password.toCharArray());
    			List<MongoCredential> credentials = new ArrayList<MongoCredential>();
    			credentials.add(credential);
    			// 通过连接认证获取MongoDB连接
    			MongoClient mongoClient = new MongoClient(addresses, credentials);
    			mongoDatabase = mongoClient.getDatabase(database);
    			Log.logInfo(mongoDatabase);
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}
    	}
    
    public static void main(String[] args) {
    		MongoDB2 db=new MongoDB2();
    		db.mongoConnect(); //确认连接正确
    }
    

      连接成功后会显示mogodb的id,错误会显示认证失败

    连接失败的案例

    成功会显示

    2.对mogodb进行数据的插入

    封装的方法insertCollection,插入可数字,字符串,

    	public boolean insertCollection(String collectionName, List<Document> documents) {
    		boolean insertResult = false;
    
    		try {
    			MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
    			collection.insertMany(documents);
    			insertResult = true;
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}
    		return insertResult;
    	}
    
    public static void main(String[] args) {
    		MongoDB2 db=new MongoDB2();
    		db.mongoConnect(); //确认连接正确
    		Date day=new Date();    
    		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		//生成json字符串
    		JSONObject json = new JSONObject();
    		json.put("id","1");
    		json.put("name","张三");
    		json.put("pwd","123456");
    		System.out.println(json);
    		
    		Document testDocument = new Document();
    		testDocument.put("times", df.format(day));//插入时间
    		testDocument.put("name","zhangjun" ); //插入名称
    		testDocument.put("info", json.toString()); //插入json字符串
    		
    		List<Document> documents = new ArrayList<Document>();
    		
    		documents.add(testDocument);
    		db.insertCollection("test_log_info", documents);
    	}
    

      

     

    3.查询数据

    /**
    	 * 获取集合
    	 * 
    	 * @param collectionName
    	 *            集合名
    	 * @param testDocument
    	 *            条件 , 支持多对条件
    	 * @return
    	 * 
    	 */
    	public MongoCursor<Document> getCollection(String collectionName, Document testDocument) {
    
    		MongoCursor<Document> mongoCursor = null;
    		try {
    			MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
    			FindIterable<Document> resultDocument = collection.find(testDocument);
    			mongoCursor = resultDocument.iterator();
    		} catch (Exception e) {
    			// TODO: handle exception
    			e.printStackTrace();
    		}
    		return mongoCursor;
    	}
    
    public static void main(String[] args) {
    		MongoDB2 db=new MongoDB2();
    		db.mongoConnect(); //确认连接正确
    		
    		Document testDocument = new Document();
    		testDocument.put("name", "zhangjun");
    		MongoCursor<Document> resultDocument = db.getCollection("test_log_info", testDocument);
    		while(resultDocument.hasNext()){  
    			System.out.println(resultDocument.next());//获取所有
    			System.out.println(resultDocument.next().get("_id")); //获取某个值  
            } 
    }

  • 相关阅读:
    javascript 中的暗物质 闭包
    关于使用 jBox 对话框的提交问题
    Orchard 的项目结构解决方案文件夹的原理与使用
    翻译:创建 Windows8 应用 Part I: Hello, world!
    翻译:FireBug 1.10 新特性
    SQL数据库设计经验
    我的女孩
    在WINDOWS下安装MRTG全攻略网络流量监控
    ASP实用函数库
    DIV CSS设计时IE6、IE7、FF 与兼容性有关的特性
  • 原文地址:https://www.cnblogs.com/chongyou/p/9243594.html
Copyright © 2011-2022 走看看