1. 程式人生 > >Mongodb-使用javaDriver 實現增刪改查

Mongodb-使用javaDriver 實現增刪改查

package Util;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;

public class MongodbHelper {
    
	//mongo連線
	private  Mongo mo=null;
	//資料庫
	private  DB db=null;
	//集合

	
    //資料庫名
    private  String dbname=null;
    private  String host=null;
    private  int port=27017;
    
	public MongodbHelper(){}
	
	public MongodbHelper(String host,int port,String dbName){
		
		this.dbname=dbName;
		this.host=host;
		this.port=port;
		createInstence(host, port);
		
	}

	/**
	 * 建立物件
	 * @param host
	 * @param port
	 * @return
	 */
	public  Mongo createInstence(String host,int port){
		
		if(mo==null){
			try {
				mo=new Mongo(host, port);
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (MongoException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return mo;
	}
	
	/*
	 * 連線資料庫
	 */
	private  DB connDb(){
		
		if(mo==null){
			System.out.println("連線失敗");
			return null;
		}else{
			if(db==null && dbname!=null){
				db=mo.getDB(this.dbname);
			}
		}
		return db;
	}
	
	
    /**
     *  建立集合
     * @param collName
     * @return
     */
	public  boolean createColl(String collName){
		
	    boolean flag=false;
	    if(db==null){
	    	connDb();
	    }
		if(db.collectionExists(collName)){
			System.out.println("集合已經存在!");
			return flag;
		}else{
			try{
		       db.createCollection(collName, null);
		       flag=true;
			}catch(Exception e){
				System.out.println("集合建立失敗:異常資訊:"+e.getMessage());
				return flag;
			}finally{
				closeMongo();
			}
		
		}
		return flag;
	}
	
	
    /**
     * 得到集合
     * @param collName
     * @return
     */
	
	public DBCollection getColl(String collName){
		if(mo==null){
			createInstence(host, port);
		}
	    if(db==null){
	    	connDb();
	    }
		DBCollection dbColl=db.getCollection(collName);
		return dbColl;
	}
	
	/**
	 * 新增 單個
	 * @param collName
	 * @param obj
	 * @return
	 */
	public boolean addDbobject(String collName,BasicDBObject obj){
		
		DBCollection coll=getColl(collName);
		try{
		   coll.insert(obj,WriteConcern.NONE);
		   return true;
		}catch (Exception e) {
			return false;
		}finally{
			this.closeMongo();
		}
	
	}
	
	/**
	 * 新增多個
	 * @param collName
	 * @param objlist
	 * @return
	 */
	public boolean addDbobject(String collName,List<DBObject> objlist){
		
		DBCollection coll=getColl(collName);
		try{
		   coll.insert(objlist);
		   return true;
		}catch (Exception e) {
			return false;
		}finally{
			this.closeMongo();
		}
	
	}

	/**
	 * 查詢多條
	 * @param collName
	 * @return
	 */
	public List<DBObject> getlist(String collName){
		
		List<DBObject> list=new ArrayList<DBObject>();
	  try{
		DBCollection coll=getColl(collName);
	    DBCursor cursor=coll.find();
	    while(cursor.hasNext()){
	    	list.add(cursor.next());
	    }
	    return list;
	  }catch (Exception e) {
		System.out.println("查詢出錯了!"+e.getMessage());
		return null;
	  }finally{
		  this.closeMongo();
	  }
	}
	
	/**
	 * 查尋單個記錄
	 * @param collName
	 * @param bobj
	 * @return
	 */
	public DBObject getdbobj(String collName,BasicDBObject bobj){
		DBCollection coll=getColl(collName);
		try{
			
		    DBObject ob=coll.findOne(bobj);
			
			return ob;
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("查詢單條失敗!"+e.getMessage());
			return null;
		}finally{
			this.closeMongo();
		}
	}
	
    
	/**
	 * 刪除操作
	 * @param collName
	 * @param bobj
	 * @return
	 */
	public boolean rvdbobj(String collName,BasicDBObject bobj){
		
		DBCollection coll=getColl(collName);
		try{
			coll.remove(bobj);
			return true;
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("刪除失敗!"+e.getMessage());
			return false;
		}finally{
			this.closeMongo();
		}
	}
	
	/**
	 * 修改資訊
	 * @param collName
	 * @param query
	 * @param o
	 * @return
	 */
	public boolean upbobj(String collName,BasicDBObject query,BasicDBObject o){
		DBCollection coll=getColl(collName);
		
		try{
			coll.update(query, o);
		    return true;
		}catch (Exception e) {
			System.out.println("更新修改操作失敗!"+e.getMessage());
			return false;
		}finally{
			this.closeMongo();
		}
		
	}
	
	
	
	//關閉連線
	public  void closeMongo(){
		// mo.close();
	}
	
	
}