How To Connect Java to MongoDB Using Eclipse IDE
Java is one of the most widely-used programming languages in the MongoDB user community. If you’re planning to work with MongoDB as a Java developer, one of the first tasks you’ll need to accomplish is to connect Java with your MongoDB instance. In this step-by-step tutorial, we’ll explain how to connect a Java application to MongoDB using the Eclipse IDE.
Before we attempt to connect a Java application with MongoDB, it’s important to make sure certain prerequisites are in place. For this task, there are a few key system requirements:
First, you need to make sure that both MongoDB and the MongoDB Java driver are properly configured beforehand.
To determine if you have the MongoDB driver is installed, use the following command:
pecl search mongo
- You’ll receive a response like the one shown below:
Package Stable/(Latest) Local
mongo 1.6.16 (stable) MongoDB database driver
mongodb 1.6.0alpha1 (alpha) 1.5.3 MongoDB driver for PHP
- Next, you need to ensure that Java JDK 8 or above is properly installed and configured beforehand. To see what version of Java is installed on your machine, just type
java -versionin the terminal window.
OpenJDK Runtime Environment (build 1.8.0_191-8u191-b12-2ubuntu0.18.04.1-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
In this tutorial, we’ll be using the Eclipse IDE, so be sure that Eclipse is properly installed and configured before you begin.
Last but not least, be sure to download the following required drivers before beginning this tutorial:
Configure The Eclipse IDE
Once you’ve confirmed that all the system requirements are in place, you can begin to configure the Eclipse IDE> First, open the Eclipse IDE application, then click on the following menus: File, then New, and finally on Java Project.
In the New Java Project window, you should see the Project name: field. Type your preferred project name in this field. In this tutorial, we’ll be using the name “MongoTest”, as seen in the below image (Figure 1).
![Figure 1](https://i.gyazo.com/290b6ad25e9026883d7c0cf1fa8450cb.png “Figure 1”)
Next, click on the Finish button in the lower right corner of the window to submit the details. The project “MongoTest” will be created and will now be reflected on the Package Explorer panel.
At this point, you can right-click on the MongoTest project, then click on the Build Path menu. After that, select Configure Build Path, and the Java Build Path window will appear.
Once you’re in the Build Path window, click on the Libraries tab, and then click on the Add External JARs button to select the drivers you downloaded earlier. After you’ve selected all the necessary “.jar” files, click Ok to close the window.
Finally, click Apply and Close to close the Java Build Path window, as shown in the below image (Figure 2):
![Figure 2](https://i.gyazo.com/561f030a43f65282d60284508dbba4ff.png “Figure 2”)
This completes our configuration of the Eclipse IDE
Connecting Java Application to MongoDB
Now that we’ve configured the Eclipse IDE to work with the MongoDB drivers, we can try connecting a Java application to MongoDB. To do this, you’ll need to first create a Java class– right click on the MongoTest project, then click New, and then click Class.
The New Java Class window should appear, In the Name field, type in the name “MongoConnection”, then select the check box for “public status void main(Stingargs)”. Finally, click the Finish button to create the file and close the window, as shown in the image below (Figure 3):
![Figure 3](https://i.gyazo.com/0f7de8c6bed5d204aa085c0cb2852a39.png “Figure 3”)
At this point, you can use the code shown below to connect the Java application to MongoDB:
MongoClient mongo = new MongoClient("127.0.0.1", 27017);
This code instantiates MongoClient and provides the localhost address, as well as the port number, for MongoDB.
If you see a red squiggly line under
MongoClient, this means that the resource needs to be imported. To do this, simply press Ctrl + Shift + O on your keyboard to import the resource.
import com.mongodb.MongoClient; statement will then appear on the top part of the code pane, as can be seen in the following images (Figure 4 and Figure 5):
![Figure 4](https://i.gyazo.com/02814fa07a9e46aee054d2df9967d340.png “Figure 4”)
To test whether the connection was a success, add the following code, which prints a response if the connection is established:
To run your Java application, click on the Run menu, then select Run as, then click Java Application:
![Figure 5](https://i.gyazo.com/2171e1ed5e4059a9979e8c4ea08384e1.png “Figure 5”)
The above image shows the output which confirms that the Java application was able to connect with MongoDB.
Java is widely used to work with MongoDB, so it’s important to know how to connect your Java application to MongoDB. Fortunately, this is an easy task to accomplish, especially if you’re using Eclipse as your IDE. With the step-by-step instructions included in this tutorial, you’ll have no problem connecting a Java application to MongoDB using Eclipse.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.Get Started