Friday, 6 December 2013

Connect to MongoDB As a Service via Java Program

In this example, I am going to show you how to connect to MongoDB as a service via our Java program in 5 simple steps.

First of all, let us know what is MongoDB as a service. This means that our MongoDB is offered as a service online from the creators of MongoDB i.e. the database will be online in their computer (server) and that we will store our data there without the need of installing database (MongoDB here) in our system (where our program exists). Clear? OK. Let us now move on to the program.

Signing Up for MongoDB as a service

  1. For this, first we need to Sign Up to the database-as-a-service (DaaS) from MongoLab. In the free account you get 500 MB for free which is enough for our practice.
  2. After signing up, you will receive a confirmation email. After confirming, you can click on the Login button to login to the website.
  3. Now you need to create a database. For this click on the Create Database link in the URL.
  4. Select the Development (single node) tab and select the FREE version. You will see some cloud providers like amazon, rackspace, windows azure etc to choose your platform. You don't need to worry about them much now. It is where your database exists, nothing more that.
  5. Now, give a database name which you will be using in the program.
  6. Now click on Create new MongoDB deployment button to complete the process.
  7. Now click on the database you have created and click on Add collection. A dialog appears, give the collection a name and click on Create.
  8. Now, you can see an yellow box which says that
  9. A database user is required to connect to the database. Click here to create a new one.
  10. Click on that, give a user name, password and password confirmation. That's it. This is your database user name and password which you will use in your program. Don't check the option read-only, if you do that user will be unable to insert data into that database.
In a rounded box above, you could see something like this:
mongodb://<dbuser>:<dbpassword>@ds023288.mongolab.com:23288/sample

This is called as the database URL with which you can connect to the database in the Mongo DB. In the above url, you need to replace <dbuser> and <dbpassword> with the database Username and password that you have given in the dialog.
Here my database name is sample (see what you have kept) and my collection name is mycollection

Connecting to MongoDB via program

Now, you know five things:
1. Your database name, collection name, database username, password and the database URL.

Before we continue, we need to download the API for Java. Download this mongo 2.10.1.jar and include it in your class path. Now let us go into the program.


import com.mongodb.*;
class MongoConnect
{
    public static void main(String args[]) throws Exception
    {
        // The text uri
        // "mongodb://xyz:MyPass_XYZ@ds023288.mongolab.com:23288/sample";
        // Give the url of your database by replacing
        // xyz with your username and MyPass_XYZ with your password
        String textUri = "mongodb://xyz:MyPass_XYZ@ds023288.mongolab.com:23288/sample";

        // Create MongoClientURI object from which you get MongoClient obj
        MongoClientURI uri = new MongoClientURI(textUri);

        // Connect to that uri
        MongoClient m = new MongoClient(uri);

        // get the database named sample
        DB d=m.getDB("sample");

        // get the collection mycollection in sample
        DBCollection collection = d.getCollection("mycollection");

        // Now create a simple BasicDBObject
        BasicDBObject b=new BasicDBObject();
        b.put("name","Gowtham Gutha");
        b.put("site","java-demos.blogspot.com");

        // Insert that object into the collection
        collection.insert(b);

    }
}

The BasicDBObject can take multiple key-value pairs which will be inserted into the collection. The insert method inserts all the key-value pairs associated with that collection object. To confirm whether the values are inserted you can go to the page (or refresh it) and you can see them.


https://mongolab.com/databases/your_database_name/collections/your_collection_name

Replace, your_database_name with the name of your database (here it is sample) and your_collection_name with the name of collection (here it is mycollection) and every thing is same. You must be logged in to see these. Remember.

So, in this way, we can simply connect and use MongoDB as a service from our Java program.

No comments:

Post a Comment