当前位置: 移动技术网 > IT编程>数据库>MongoDB > MongoDB系列教程(六):java操作mongodb实例

MongoDB系列教程(六):java操作mongodb实例

2017年12月08日  | 移动技术网IT编程  | 我要评论

java操作mysql数据库的代码我们已经了如指掌了,增删改查,java对mongodb数据库也是类似的操作,先是数据库连接,再是进行操作。

首先我们进入进入admin数据库,然后建立自己的数据库testmongodb,进入admin数据库后,就可以直接进入testmongodb,因为用户可以进入系统的数据库,就是超级管理员,use testmongodb后,为该数据库设置用户名和密码,db.adduser('root','root'),这样我们在程序中连该数据库,并实现增删改查,代码如下所示。

代码如下所示:

复制代码 代码如下:

package com.mkyong.core; 
 
import java.net.unknownhostexception; 
import java.util.date; 
import com.mongodb.basicdbobject; 
import com.mongodb.db; 
import com.mongodb.dbcollection; 
import com.mongodb.dbcursor; 
import com.mongodb.mongo; 
import com.mongodb.mongoexception; 
 
/**
 * java + mongodb hello world example
 * 
 */ 
public class app { 
    public static void main(string[] args) { 
 
        try { 
 
            /**** connect to mongodb ****/ 
            // since 2.10.0, uses mongoclient 
            //mongoclient mongo = new mongoclient("localhost", 27017); 
             
            mongo mongo = new mongo("127.0.0.1",27017); 
             
           
            /**** get database ****/ 
            // if database doesn't exists, mongodb will create it for you 
            db db = mongo.getdb("testmongodb"); 
            //database username  root  and password root  
            boolean ok = db.authenticate("root","root".tochararray()); 
            if(ok){ 
                system.out.println("db connection success!"); 
                 
            }{ 
                system.out.println("db connection fail !"); 
            } 
            /**** get collection / table from 'testmongodb' ****/ 
            // if collection doesn't exists, mongodb will create it for you 
            dbcollection table = db.getcollection("user"); 
 
            /**** insert ****/ 
            // create a document to store key and value 
            basicdbobject document = new basicdbobject(); 
            document.put("name", "mkyong"); 
            document.put("age", 30); 
            document.put("createddate", new date()); 
            table.insert(document); 
 
            /**** find and display ****/ 
            basicdbobject searchquery = new basicdbobject(); 
            searchquery.put("name", "mkyong"); 
 
            dbcursor cursor = table.find(searchquery); 
 
            while (cursor.hasnext()) { 
                system.out.println(cursor.next()); 
            } 
 
            /**** update ****/ 
            // search document where name="mkyong" and update it with new values 
            basicdbobject query = new basicdbobject(); 
            query.put("name", "mkyong"); 
 
            basicdbobject newdocument = new basicdbobject(); 
            newdocument.put("name", "mkyong-updated"); 
 
            basicdbobject updateobj = new basicdbobject(); 
            updateobj.put("$set", newdocument); 
 
            table.update(query, updateobj); 
 
            /**** find and display ****/ 
            basicdbobject searchquery2  
                = new basicdbobject().append("name", "mkyong-updated"); 
 
            dbcursor cursor2 = table.find(searchquery2); 
 
            while (cursor2.hasnext()) { 
                system.out.println(cursor2.next()); 
            } 
 
            /**** done ****/ 
            system.out.println("done"); 
 
        } catch (unknownhostexception e) { 
            e.printstacktrace(); 
        } catch (mongoexception e) { 
            e.printstacktrace(); 
        } 
 
    } 

控制台输入结果如下:

复制代码 代码如下:

    db connection success!
    db connection fail !
{ "_id" : { "$oid" : "544073c4d58dfa6e469555ba"} , "name" : "mkyong" , "age" : 30 , "createddate" : { "$date" : "2014-10-17t01:41:24.479z"}}
{ "_id" : { "$oid" : "543e154bd58d704982fd38f0"} , "name" : "mkyong-updated" , "age" : 30 , "createddate" : { "$date" : "2014-10-15t06:33:47.321z"}}
{ "_id" : { "$oid" : "5440719dd58d08a207605c8e"} , "name" : "mkyong-updated" , "age" : 30 , "createddate" : { "$date" : "2014-10-17t01:32:13.922z"}}
{ "_id" : { "$oid" : "544073c4d58dfa6e469555ba"} , "name" : "mkyong-updated" , "age" : 30 , "createddate" : { "$date" : "2014-10-17t01:41:24.479z"}}
done


源码下载:

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网