国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MongoDB > 正文

MongoDB系列教程(六):java操作mongodb實(shí)例

2020-10-29 18:51:40
字體:
供稿:網(wǎng)友

java操作mysql數(shù)據(jù)庫的代碼我們已經(jīng)了如指掌了,增刪改查,java對(duì)mongodb數(shù)據(jù)庫也是類似的操作,先是數(shù)據(jù)庫連接,再是進(jìn)行操作。

首先我們進(jìn)入進(jìn)入admin數(shù)據(jù)庫,然后建立自己的數(shù)據(jù)庫testMongoDb,進(jìn)入admin數(shù)據(jù)庫后,就可以直接進(jìn)入testMongoDb,因?yàn)橛脩艨梢赃M(jìn)入系統(tǒng)的數(shù)據(jù)庫,就是超級(jí)管理員,use testMongoDb后,為該數(shù)據(jù)庫設(shè)置用戶名和密碼,db.addUser('root','root'),這樣我們在程序中連該數(shù)據(jù)庫,并實(shí)現(xiàn)增刪改查,代碼如下所示。

代碼如下所示:

復(fù)制代碼 代碼如下:

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(); 
        } 
 
    } 

控制臺(tái)輸入結(jié)果如下:

復(fù)制代碼 代碼如下:

    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


源碼下載:http://xiazai.VeVB.COm/201503/other/mongodb_helloworld.zip

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 灵石县| 台前县| 仙游县| 隆林| 图木舒克市| 通辽市| 晋中市| 湾仔区| 静海县| 六安市| 信阳市| 澜沧| 确山县| 郁南县| 德阳市| 资兴市| 张北县| 乌兰察布市| 杭锦旗| 工布江达县| 襄樊市| 迁西县| 富源县| 巴林右旗| 和政县| 惠水县| 浏阳市| 棋牌| 长乐市| 昌邑市| 富锦市| 萍乡市| 大连市| 肃宁县| 三穗县| 图们市| 甘洛县| 临泽县| 义乌市| 南平市| 黔江区|