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
Description
To associate with the database, determine the database name into the classpath, if database is not determine then mongodb makes it consequently.
Examples
By viewing the below example, one can easily understand how to connect to a database.
[c]
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() );
}
}
}
[/c]
After compiling the above execution in MongoDB Java Driver the output will be as follows:
[c]$javac MongoDBJDBC.java
$java -classpath ".:mongo-2.10.1.jar" MongoDBJDBC
Connect to database successfully
Authentication: true[/c]
Creating Collections
Description
In order to create a new collections use createCollection() method in MongoDB Java Driver.
Examples
[c]
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() );
}
}
[/c]
After compiling the above execution in MongoDB Java Driver the output will be as follows:
[c]
Connect to database successfully
Authentication: true
Collection created successfully
[/c]
Summary
Key Points
MongoDB Java Driver - Install the Java drivers on MongoDB database.