Open In App

How to Use MongoDB in Eclipse?

Eclipse is an IDE (integrated development environment) used in computer programming. Eclipse is an IDE in which we can use any type of programming language for which plugins are available. It contains a base workspace and an extensible plug-in system for customizing the environment. Eclipse is free and open-source software. It is written in Java and is considered to be the most popular Java IDE. It works on all main platforms, including Linux, Windows, Mac OS, etc., and has many powerful features that can be used to work out in many projects. It also provides documentation and modeling support and offers UML, OCL, SysML, and implementation tools. Besides that, it provides support for Git, Apache Maven, Gradle, etc.

Features of Eclipse



 To find out more about Eclipse and its setup process, you can have a look here.

MongoDB

MongoDB is an open-source platform that is a document-oriented programming language (NoSQL database). And it is the leading database in NoSQL. MongoDB is written in c++. As it is a NoSQL database, it uses JSON-like documents to store the data in the database.



Advantages of MongoDB

To know more about Mongo DB and its setup process, click here.

Mongo-Java-Driver

There are many drivers for MongoDB, in that Mongo-Java-Driver is one of the drivers, which is the official Mongo Driver used for synchronous Java applications. The MongoDB Java driver API documentation contains several libraries organized by functionality. For detailed information about classes and methods in each library, see the following table for their descriptions and links to the API documentation. To find out more about Mongo-Java-Driver and its setup process, visit this tutorial.

How to use MongoDB in Eclipse

To use MongoDB in Eclipse, you have to install MongoDB. Eclipse has to be installed in the system. If you haven’t installed them,

  1. Click here to Download MongoDB.
  2. Click here to Download Eclipse IDE.

Make sure that you keep MongoDB always open during the execution of the program.

If this is shown, MongoDB has been installed on your PC

You will have an error as the mongo keyword is not recognized

 

 

 

Example program for creating a database with MongoDB in eclipse.




import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient; //including essential libraries
  
public class demo {
  
    public static void main(String[] args) throws Exception
    {
        // throwing an exception
        try {
            MongoClient mongoClient = new MongoClient("localhost",27017); // establishing a server connection in localhost
            DB db = mongoClient.getDB("student"); // creating database called student
            System.out.println("connection established"); // prints connection established if database created
            DBCollection coll = db.getCollection("student");
        }
        catch (Exception e) {
            System.out.println("error"); // if the exception occurs prints error
        }
        System.out.println("server ready"); // if there is no exception prints server ready
    }
}

We will establish a server connection with localhost, and then we will create a database with the name Student. If the database is created, then it will give output as the database created, and if an exception occurs, it will print an error.

 

So that we can create a new database, manage the database, and do all the operations on the database in eclipse using MongoDB. You can learn more about MongoDB by following this Mongo DB tutorial.


Article Tags :