Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Java with MongoDB

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 462
    Comment on it

    Java with MongoDB:- The MongoDB is the Document type Database. It is set of Collections and documents. it supports the NOSQL and to manage the collections inside the collection there are numbers of documents. The Below code will show you how to connect your java Program with MongoDB.

    NOTE:- Firstly you have to download the mongo.jar and you need to include the mongo.jar into your classpath.

    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.MongoClient;
    import com.mongodb.MongoException;
    
    
    public class Main {
    
    
      public static void main(String[] args) {
    
        try {
    
        /* Connect to MongoDB */
    
        MongoClient mongo = new MongoClient("localhost", 27017);
    
        /* Get the database name as "manish" */
        // if database doesn't exists, MongoDB will create it for you
        DB db = mongo.getDB("manish");
    
        /* Get collection / table from 'manish' ****/
        // if collection doesn't exists, MongoDB will create it for you
        DBCollection table = db.getCollection("emp");
    
        /* Insert the Document */
    
        BasicDBObject document = new BasicDBObject();
        document.put("name", "manish");
        document.put("desig", "Software Developer");
        document.put("joinDate", new Date());
        table.insert(document);
    
        /* Find and display Record */
    
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("name", "manish");
    
        DBCursor cursor = table.find(searchQuery);
    
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    
        /* search document where name="manish" and update it with "manisha" */
    
        BasicDBObject query = new BasicDBObject();
        query.put("name", "manish");
    
        BasicDBObject newDocument = new BasicDBObject();
        newDocument.put("name", "manisha");
    
        BasicDBObject updateObj = new BasicDBObject();
        updateObj.put("$set", newDocument);
    
        table.update(query, updateObj);
    
    
    
        System.out.println("Successfully Done");
    
        } catch (UnknownHostException e) {
        e.printStackTrace();
        } catch (MongoException e) {
        e.printStackTrace();
        }
    
      }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: