MongoDB - SPLessons

MongoDB Java Driver

Chapter 27

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

MongoDB Java Driver

MongoDB Java Driver

shape Description

The MongoDB Java Driver installation process requires java setup and JDBC Driver of MongoDB. And can check the java installation and MongoDB setup into the drivers by downloading the mongo.jar files into the classpath.

Connecting to the database

shape Description

To associate with the database, determine the database name into the classpath, if database is not determine then mongodb makes it consequently.

shape Examples

By viewing the below  example, one can easily understand how to connect to a database.

MongoDB shell version: 3.2.7
connecting to: test
> show dbs
admin   0.000GB
demo    0.000GB
local   0.000GB
mydb    0.000GB
testdb  0.000GB
> use admin
switched to db admin
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;

import com.mongodb.ServerAddress;
import java.util.Arrays;

public class MongoDBJDBC {

   public static void main( String args[] ) {
	
      try{
		
         // To connect to mongodb server
         MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
			
         // Now connect to your databases
         DB db = mongoClient.getDB( "test" );
         System.out.println("Connect to database successfully");
         boolean auth = db.authenticate(myUserName, myPassword);
         System.out.println("Authentication: "+auth);
			
      }catch(Exception e){
         System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      }
   }
}

After compiling the above execution in MongoDB Java Driver the output will be as follows:

$javac MongoDBJDBC.java
$java -classpath ".:mongo-2.10.1.jar" MongoDBJDBC
Connect to database successfully
Authentication: true

Creating Collections

shape Description

In order to create a new collections use createCollection() method in MongoDB Java Driver.

shape Examples


import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;

import com.mongodb.ServerAddress;
import java.util.Arrays;

public class MongoDBJDBC {

   public static void main( String args[] ) {
	
      try{   
		
         // To connect to mongodb server
         MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
			
         // Now connect to your databases
         DB db = mongoClient.getDB( "test" );
         System.out.println("Connect to database successfully");
			
         boolean auth = db.authenticate(myUserName, myPassword);
         System.out.println("Authentication: "+auth);
			
         DBCollection coll = db.createCollection("mycol");
         System.out.println("Collection created successfully");
      }catch(Exception e){
         System.err.println( e.getClass().getName() + ": " + e.getMessage() );
      }
   }

After compiling the above execution in MongoDB Java Driver the output will be as follows:

Connect to database successfully
Authentication: true
Collection created successfully

Summary

shape Key Points

  • MongoDB Java Driver – Install the Java drivers on MongoDB database.