1. MongoDB for Java driver package
Https://github.com/mongodb/mongo-java-driver/downloads
2,Online document
Http://www.mongodb.org/display/DOCS/Java+Language+Center
1. query all data in a table (called a set in MongoDB ).
Java code dbtest. Java
Package COM. archie. mongoDB; import java.net. unknownhostexception; 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; /*** query all data in the specified dbcollection set of the specified database * @ author archie2010 ** since 10:40:21 */public class dbtest {public static void main (string [] ARGs) throws unknownhostexception, unknown exception {/** Mongo instance represents a database connection pool * Mongo Mg = new Mongo ("localhost"); Mongo Mg = new Mongo ("localhost", 27017 ); */Mongo Mg = new Mongo (); // obtain the database object dB named "dbtest" = Mg. getdb ("dbtest"); // query all sets in the database (equivalent to tables) for (string name: DB. getcollectionnames () {system. out. println ("collection name:" + name);} dbcollection users = dB. getcollection ("EMP"); // query all data in the Set dbcursor cur = users. find (); system. out. println ("record count:" + cur. count (); While (cur. hasnext () {dbobject object = cur. next (); system. out. println (object); // retrieve the data system of the object whose names are 'uname' and 'upwd. out. println ("uname:" + object. get ("uname") + "\ tupwd:" + object. get ("upwd "));}}}
Running result:
2. CRUD operation on the specified dbcollection set
Java code
Dbutil. Java
Package COM. archie. mongoDB; import java.net. unknownhostexception; import COM. mongoDB. DB; import COM. mongoDB. dbcollection; import COM. mongoDB. mongo;/*** get the tool class of the dbcollection Set * @ author archie2010 ** since 10:54:42 */public class dbutil {public static Mongo Mg = NULL; public static dB DB = NULL; public static dbcollection collection;/*** get dbcollection object * @ Param dbname * @ Param colname * @ return */public static dbcollection getdbcollection (string dbname, string colname) {If (Mg = NULL) {try {Mg = new Mongo ();} catch (unknownhostexception e) {e. printstacktrace () ;}}if (DB = NULL) {DB = Mg. getdb (dbname);} return dB. getcollection (colname );}}
Crudtest. Java
Package COM. archie. mongoDB; import COM. mongoDB. basicdbobject; import COM. mongoDB. dbcollection; import COM. mongoDB. dbcursor; import COM. mongoDB. dbobject; /*** CRUD operation on the specified dbcollection * @ author archie2010 ** since 10:51:24 */public class crudtest {/*** added * @ Param OBJ */public static void add (dbobject OBJ) {dbcollection coll = dbutil. getdbcollection ("dbtest", "EMP"); Coll. insert (OBJ);}/*** Delete * @ Param OBJ */public static void Delete (dbobject OBJ) {dbcollection coll = dbutil. getdbcollection ("dbtest", "EMP"); Coll. remove (OBJ);}/*** query */public static void query () {dbcollection coll = dbutil. getdbcollection ("dbtest", "EMP"); // query all data in the Set, dbcursor cur = Coll. find (); system. out. println ("record count:" + cur. count (); While (cur. hasnext () {dbobject object = cur. next (); system. out. println (object); // retrieve the data system in which the list of objects is 'uname' and 'upwd. out. println ("uname:" + object. get ("uname") + "\ tupwd:" + object. get ("upwd") + "\ t_id:" + object. get ("_ id") ;}}/*** modify */public static void modify (dbobject orig, dbobject update) {dbcollection coll = dbutil. getdbcollection ("dbtest", "EMP"); Coll. update (orig, update, true, false);} public static void main (string [] ARGs) {dbobject empobj = new basicdbobject (); empobj. put ("uname", "Teddy"); empobj. put ("upwd", "123456"); // Add add (empobj); query (); dbobject updateobj = new basicdbobject (); updateobj. put ("uname", "Teddy"); updateobj. put ("upwd", "3333"); // update modify (New basicdbobject ("uname", "Teddy"), updateobj); system. out. println ("----------------------- after modification -------------------"); query (); // delete Delete (New basicdbobject ("uname", "Teddy"); system. out. println ("----------------------- after deletion -------------------"); query ();}}
Running effect:
Sample download