posts - 495,  comments - 11,  trackbacks - 0

          1.準(zhǔn)備

          ???? 下載Mongo Java Driver,下載地址:https://github.com/downloads/mongodb/mongo-java-driver/mongo-2.5.3.jar

          ???? 如果是使用maven編譯,可在pom.xml文件中加入如下依賴

          ???? <dependency>
          ????????? <groupId>org.mongodb</groupId>
          ????????? <artifactId>mongo-java-driver</artifactId>
          ????????? <version>2.5.3</version>
          ???? </dependency>

          2.上程序

          /**
          ?* MongoDB學(xué)習(xí)之HelloWorld
          ?*
          ?* @author <a href="mailto:gerald.chen@qq.com">GeraldChen</a>
          ?* @version $Id: HelloWorldTest.java,v 1.1 2011/05/26 12:42:45 gerald.chen Exp $
          ?*/
          public class HelloWorldTest {

          ????? /** 數(shù)據(jù)庫連接IP */
          ???? public static final String DB_HOST = "192.168.35.101";

          ???? /** 數(shù)據(jù)庫連接端口 */
          ???? public static final int DB_PORT = 27017;

          ???? public static void main(String[] args) throws Exception {
          ???????? // connect to mongoDB, ip and port number
          ???????? Mongo mongo = new Mongo(DB_HOST, DB_PORT);

          ???????? // get database from MongoDB,
          ???????? // if database doesn't exists, mongoDB will create it automatically
          ???????? DB db = mongo.getDB("test_db");

          ???????? // Get collection from MongoDB, database named "yourDB"
          ???????? // if collection doesn't exists, mongoDB will create it automatically
          ???????? DBCollection collection = db.getCollection("test_collection");

          ???????? // create a document to store key and value
          ???????? BasicDBObject document = new BasicDBObject();
          ???????? document.put("id", 1001);
          ???????? document.put("message", "hello world mongoDB in Java");

          ???????? // save it into collection named "yourCollection"
          ???????? collection.insert(document);

          ???????? // search query
          ???????? BasicDBObject searchQuery = new BasicDBObject();
          ???????? searchQuery.put("id", 1001);

          ???????? // query it
          ???????? DBCursor cursor = collection.find(searchQuery);

          ???????? // loop over the cursor and display the retrieved result
          ???????? while (cursor.hasNext()) {
          ????????????????? System.out.println(cursor.next());
          ???????? }
          ???????? System.out.println("Done");
          ???}

          }

          2.程序輸出


          關(guān)鍵詞:HelloWorld?? MongoDB?? NoSQL?? JAVA??? 程序?? 軟件?? 數(shù)據(jù)庫?? 程序員
          ?

          posted on 2011-05-26 20:47 jadmin 閱讀(103) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 桂林市| 泾源县| 确山县| 南京市| 佛坪县| 饶阳县| 正定县| 顺义区| 大渡口区| 沂源县| 贺兰县| 五河县| 建宁县| 新邵县| 晋宁县| 罗江县| 临清市| 建阳市| 枞阳县| 洮南市| 祁门县| 隆林| 泽普县| 双流县| 南丰县| 巨鹿县| 麻阳| 遂溪县| 浦东新区| 札达县| 上栗县| 繁峙县| 琼结县| 常州市| 屏山县| 中宁县| 健康| 高碑店市| 张家口市| 惠安县| 六安市|