Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7099499
  • 博文数量: 3857
  • 博客积分: 6409
  • 博客等级: 准将
  • 技术积分: 15948
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-02 16:48
个人简介

迷彩 潜伏 隐蔽 伪装

文章分类

全部博文(3857)

文章存档

2017年(5)

2016年(63)

2015年(927)

2014年(677)

2013年(807)

2012年(1241)

2011年(67)

2010年(7)

2009年(36)

2008年(28)

分类: 信息化

2013-03-14 22:45:29

1、创建仅有数据库实例DB package com.boonya.mongo;
import java.net.UnknownHostException;
import java.util.Arrays;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;

  
public class DBConnection {
	
	private DBConnection(){}
	
	private static String  userName="boonya";
	private static char [] password="boonya".toCharArray();
	private static  DB db=null;
	
	/**
	 * mongoClient = new MongoClient();
         * // or
         * mongoClient = new MongoClient( "localhost" );
         * // or
         * mongoClient = new MongoClient( "localhost" , 27017 );
         * // or, to connect to a replica set, supply a seed list of members
	 * @return
	 */
	public synchronized static DB getInstance(){
        if(db==null){
    		MongoClient mongoClient=null;
    		try {
    			mongoClient = new MongoClient(Arrays.asList(new ServerAddress("localhost", 27017),
    					new ServerAddress("localhost", 27018),
    					new ServerAddress("localhost", 27019)));
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		}
    		db = mongoClient.getDB("mydb");
    		boolean auth = db.authenticate(userName,password);
            if(!auth){                      //the authentication is invalid
            	return null;
            }
        }
		return db;
	}

} 2、创建CRUD操作东西类 package com.boonya.mongo.optimize;
import java.net.UnknownHostException;
import com.boonya.mongo.utils.ConstantUtils;
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.MongoOptions;


public class MongDbCRUD {
	
	private static Mongo mongo = null;
	private static DB db;
	private static DBCollection table;
	
	private static MongDbCRUD instace;
	
	public static synchronized MongDbCRUD getInstance(){
		if(instace==null||mongo==null||db==null||table==null){
			instace = new MongDbCRUD();
		}
		return instace;
	}
	
	public MongDbCRUD(){
		String host = ConstantUtils.getValue("mongo.host").toString();
		int port = Integer.parseInt(ConstantUtils.getValue("mongo.port").toString());
		String datablease = ConstantUtils.getValue("mongo.datablease").toString();
		String tablelename = ConstantUtils.getValue("mongo.table.bound").toString();
		
		try {
			mongo = new Mongo(host, port);
			// 连接池
			MongoOptions opt = mongo.getMongoOptions();
			opt.connectionsPerHost =  10  ; //poolsize
			opt.threadsAllowedToBlockForConnectionMultiplier =  10 ;
			
			//获取temp DB;如果默许没有创建,mongodb会自动创建
			db = mongo.getDB(datablease);
			//获取users DBCollection;如果默许没有创建,mongodb会自动创建
			table = db.getCollection(tablelename);
			//DO SOMETHING
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (MongoException e) {
			e.printStackTrace();
		}
	}
	
	public MongDbCRUD(String host, int port, String datablease, String tablelename){
		try {
			mongo = new Mongo(host, port);
			// 连接池
			MongoOptions opt = mongo.getMongoOptions();
			opt.connectionsPerHost =  10  ; //poolsize
			opt.threadsAllowedToBlockForConnectionMultiplier =  10 ;
			
			//获取temp DB;如果默许没有创建,mongodb会自动创建
			db = mongo.getDB(datablease);
			//获取users DBCollection;如果默许没有创建,mongodb会自动创建
			table = db.getCollection(tablelename);
			//DO SOMETHING		
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (MongoException e) {
			e.printStackTrace();
		}
	}
	
	public static void free(){
		if(mongo!=null){
			mongo.close();
		}
		mongo = null;
		db = null;
		table = null;
		System.gc();
	}
	
	public static DBCollection getDBCollection(){
		return table;
	}
	
	public void add(DBObject row){
		table.save(row);
	}
	
	public void query(){
		DBCursor cur = table.find();
		while (cur.hasNext()) { 
			DBObject dbobject = cur.next();
			System.out.println(dbobject.get("key"));//get value
		} 
	}
	
}
/*注:mongo.properties的内容如下,
#mongodb setting
mongo.host=192.168.20.38
mongo.port=27017
mongo.database=map
mongo.table.bound=bound
mongo.username=username
mongo.pwd=pwd*/  
阅读(910) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~