NoSql之MongoDB實現資料庫資料操作

來源:互聯網
上載者:User

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  ; //poolsizeopt.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  ; //poolsizeopt.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 settingmongo.host=192.168.20.38mongo.port=27017mongo.database=mapmongo.table.bound=boundmongo.username=usernamemongo.pwd=pwd*/
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.