MongoDB for database data operations

Source: Internet
Author: User

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 */
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.