1. Create a unique database instance dB
Package COM. boonya. mongo; import java.net. unknownhostexception; import Java. util. arrays; import COM. mongoDB. DB; import COM. mongoDB. using client; 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;/*** Consumer Client = new Consumer Client (); * // or * Consumer CLI Ent = new guest client ("localhost"); * // or * guest client = new guest client ("localhost", 27017); * // or, to connect to a replica set, supply a seed list of members * @ return */Public synchronized static dB getinstance () {If (DB = NULL) {your client then client = NULL; try {export client = new export client (arrays. aslist (New serveraddress ("localhost", 27017), new serveraddress ("localhost", 27018), n EW serveraddress ("localhost", 27019);} catch (unknownhostexception e) {e. printstacktrace ();} dB = Consumer Client. getdb ("mydb"); Boolean auth = dB. authenticate (username, password); If (! Auth) {// The authentication is invalid return NULL ;}} return dB ;}}
2. Create a crud tool
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. except exception; import COM. mongoDB. optional options; public class mongdbcrud {Private Static Mongo = NULL; Private Static dB; priva Te 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 = constant Utils. getvalue ("Mongo. datablease "). tostring (); string tablelename = constantutils. getvalue ("Mongo. table. bound "). tostring (); try {Mongo = new Mongo (host, Port); // connection pool selection options opt = Mongo. getaskoptions (); OPT. connectionsperhost = 10; // poolsizeopt. threadsallowedtoblockforconnectionmultiplier = 10; // get temp dB; if it is not created by default, MongoDB will automatically create DB = Mongo. getdb (datablease); // get users dbcollection; if it is not created by default, Mongo The database automatically creates table = dB. getcollection (tablelename); // do something} catch (unknownhostexception e) {e. printstacktrace ();} catch (except exception e) {e. printstacktrace () ;}} public mongdbcrud (string host, int port, string datablease, string tablelename) {try {Mongo = new Mongo (host, Port ); // connection pool selection options opt = Mongo. getaskoptions (); OPT. connectionsperhost = 10; // poolsizeopt. threadsallowedtoblockforconne Ctionmultiplier = 10; // get temp dB. If no temp dB is created by default, MongoDB will automatically create DB = Mongo. getdb (datablease); // get users dbcollection; if no database is created by default, MongoDB automatically creates table = dB. getcollection (tablelename); // do something} catch (unknownhostexception e) {e. printstacktrace ();} catch (except exception 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 = cur. next (); system. out. println (dbobject. get ("key"); // get value }}/ * Note: Mongo. the properties content is as follows: # MongoDB settingmongo. host = 192.168.20.382.16.port = 27017mongo. database = mapmongo. table. bound = boundmongo. username = usernamemongo. pwd = Pwd */