Java Language Binding for MongoDB using NetBeans

Create a Maven Project and edit the pom.xml file and add the following lines inside the project tag at last.

<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.4.2</version>
</dependency>
</dependencies>

Now right click on the project name and click build with dependencies to load the driver.

Now create a java file named javamongodb and copy paste the following code.

import com.mongodb.client.MongoCollection; 
import com.mongodb.client.MongoDatabase; 
import com.mongodb.client.FindIterable; 
import com.mongodb.client.model.Filters; 
import com.mongodb.client.model.Updates;
import com.mongodb.client.MongoCursor; 
import org.bson.Document;  
import com.mongodb.MongoClient; 
import com.mongodb.MongoCredential; 
import java.util.;
import java.io.;
import java.util.logging.Logger;
import java.util.logging.Level; 
public class javamongodb { 
 public static void main( String args[] ) {  
       try
       {
           Logger mongoLogger = Logger.getLogger( "org.mongodb.driver" );
             mongoLogger.setLevel(Level.SEVERE); // e.g. or Log.WARNING, etc.
           // Creating a Mongo client 
           Scanner sc=new Scanner(System.in);
           MongoClient mongo = new MongoClient( "localhost" , 27017 ); 
           System.out.println("Connected to the Server successfully");  

      System.out.println("\nList of available databases:");
      MongoCursor<String> dbsCursor = mongo.listDatabaseNames().iterator();
      while(dbsCursor.hasNext())
          {
            System.out.println(dbsCursor.next());
          }      

      System.out.println("\nEnter Database Name:");
      String db=sc.next();
      // Accessing the database 
      MongoDatabase database = mongo.getDatabase(db);

      System.out.println("\nAvailable collections in "+db);
      for (String col : database.listCollectionNames()) { 
        System.out.println(col); 
        } 
      System.out.println("Enter Collection Name:");
      String coll=sc.next();
      MongoCollection<Document> collection = database.getCollection(coll);
      while (true)
      {
          System.out.println("1.Insert\n2.View\n3.Update\n4.Delete\n5.Exit");
          System.out.println("Enter your Choice");
          int ch=sc.nextInt();
          switch(ch)
          {
              case 1:
              System.out.println("Enter key :");
              String key=sc.next();
              System.out.println("Enter value :");
              String value=sc.next();
              Document doc=new Document(key,value);
              collection.insertOne(doc);
              System.out.println("\n\nInserted Successfully\n\n");
              break;

              case 2:
              FindIterable<Document> iterDoc = collection.find(); 
              Iterator it = iterDoc.iterator(); 
              while (it.hasNext()) 
              {  
                System.out.println(it.next());  

              } 
              break;

              case 3:
              System.out.println("Enter Update Criteria..");
              System.out.println("Enter key :");
              key=sc.next();
              System.out.println("Enter value :");
              value=sc.next();
              System.out.println("Enter Update Values..");
              System.out.println("Enter Update key :");
              String ukey=sc.next();
              System.out.println("Enter Update value :");
              String uvalue=sc.next();
              collection.updateOne(Filters.eq(key, value), Updates.set(ukey, uvalue));       
              System.out.println("Document update successfully...");  
              break;

              case 4:
              System.out.println("Enter Delete Criteria..");
              System.out.println("Enter key :");
              key=sc.next();
              System.out.println("Enter value :");
              value=sc.next();
              collection.deleteOne(Filters.eq(key, value)); 
              System.out.println("Document deleted successfully...");
              break;

              case 5:
              System.exit(0);
              break;

              default:
              System.out.println("Invalid Choice..\nTry Again");


      }


  }
  }
  catch (Exception e) 
  {
        e.printStackTrace();
  }
}
}

Leave a comment