How to connect mongodb atlas with java
Docs Home → Java Show On this page
This guide shows you how to create an application that uses the Java driver to connect to a MongoDB Atlas cluster. If you prefer to connect to MongoDB using a different driver or programming language, see our list of official MongoDB drivers. The Java driver lets you connect to and communicate with MongoDB clusters from a Java application. MongoDB Atlas is a fully-managed cloud database service that hosts your data on MongoDB clusters. In this guide, we show you how to get started with your own free (no credit card required) cluster. Consult the following steps to connect your Java application with a MongoDB Atlas cluster. Make sure that your system has JDK 8 or later installed. For more information on how to check your version of Java and install the JDK, see the Oracle Overview of JDK Installation documentation. This guide shows you how to add the MongoDB Java driver dependencies using Maven or Gradle. We recommend that you use an integrated development environment (IDE) such as Intellij IDEA or Eclipse IDE make it more convenient to configure Maven or Gradle to build and run your project. If you are not using an IDE, see Building Maven or Creating New Gradle Builds for more information on how to set up your project. If you are using Maven, add the following to your
If you are using
Gradle, add the following to your
Once you configure your dependencies, ensure they are available to your project which may require running your dependency manager and refreshing the project in your IDE. After setting up your Java project dependencies, create a MongoDB cluster where you can store and manage your data. Complete the Get Started with Atlas guide to set up a new Atlas account, create and launch a free tier MongoDB cluster, load datasets, and interact with the data. After completing the steps in the Atlas guide, you should have a new MongoDB cluster deployed in Atlas, a new database user, and sample datasets loaded into your cluster. In this step, we create and run an application that uses the MongoDB Java driver to connect to your MongoDB cluster and run a query on the sample data. We pass instructions to the driver on how to connect to your MongoDB cluster in a string called the connection string. This string includes information on the hostname or IP address and port of your cluster, authentication mechanism, user credentials when applicable, and other connection options. If you are connecting to an instance or cluster that is not hosted by Atlas, see Other Ways to Connect to MongoDB for instructions on how to format your connection string. To retrieve your connection string for the cluster and user you created in the previous step, log into your Atlas account and navigate to the Clusters section and click the Connect button for the cluster that you want to connect to as shown below. Proceed to the Connect Your Application step and select the Java driver. Select "4.1 or Later" for the version. Click the Copy icon to copy the connection string to your clipboard as shown below. Save your Atlas connection string in a safe location that you can access for the next step. Next, create a file to contain your application called
When you run the
If you receive no output or an error, check whether you included the proper connection string in your Java class, and whether you loaded the sample dataset into your MongoDB Atlas cluster. ImportantKnown connection issue when using TLS v1.3If you encounter an error connecting to your MongoDB instance or cluster that resembles the following while running your application, you may need to update your JDK to the latest patch release:
This exception is a known issue when using the TLS 1.3 protocol with specific versions of JDK, but was fixed for the following releases:
To resolve this error, update your JDK to one of the preceding patch versions or a newer one. After completing this step, you should have a working application that uses the Java driver to connect to your MongoDB cluster, run a query on the sample data, and print out the result. In the previous section, you ran a query on a sample collection to retrieve data in the map-like class Create a file called
Create a new file
When you run the
If you receive no output or an error, check whether you included the proper connection string in your Java class, and whether you loaded the sample dataset into your MongoDB Atlas cluster. See the following links for more information on using POJOs to store and retrieve data:
Learn how to read and modify data using the Java driver in our Fundamentals CRUD guide or how to perform common operations from our Usage Examples. How does MongoDB connect to Java program?Learn how to set up a connection and specify connection behavior from your application to a MongoDB deployment using the driver in the following sections:. Connect to MongoDB.. View a List of Connection Options.. Specify Connection Behavior with the MongoClient Class.. Enable Network Compression.. Enable TLS/SSL on a Connection.. How do I connect to Atlas MongoDB?Click on Connect.. Choose Connect Your Application.. Choose your driver and driver version. ... . Copy the generated connection string. ... . Whitelist your IP address. ... . Click on Add current IP address. ... . Are you connecting from another IP address, through a VPN, or getting errors? ... . Name your connection and click on the From URI button.. Is MongoDB compatible with Java?To interact with MongoDB from our Java application, we will use a piece of software called the Java Driver, which is an official and recommended MongoDB driver application for the Java language.
Can JDBC connect to MongoDB?Using the MongoDB JDBC connectivity, it's easier to place a query with the database, introduce updates to the database, and call upon stored processes. In this tutorial article, we will explore the MongoDB JDBC connect in detail.
|