Docs Menu
Docs Home
/
MongoDB Atlas
/ / /

Connect with JDBC Driver

On this page

  • Supported Authentication Mechanisms
  • Prerequisites
  • Procedure

This page describes how to install and configure the MongoDB JDBC Driver for connecting to a federated database instance.

You can authenticate with SCRAM-SHA-1, SCRAM-SHA-256, MONGODB-X509, and MongoDB Passwordless Authentication with AWS.

  • A federated database instance mapped to one or more data stores.

    Note

    If some or all of your data comes from an Atlas cluster, you must use MongoDB version 5.0 or greater for that cluster to take advantage of Atlas SQL.

  • An application or BI tool that you want to connect to your federated database instance with the JDBC driver.

  • The MongoDB JDBC Driver.

You can use the JDBC driver to connect to SQL-based Java applications that accept a JDBC API, such as a Maven project.

Download the latest MongoDB JDBC Driver version from the MongoDB download center if you haven't already.

1

To connect with your Maven application, copy the dependency snippet from the Maven Central Repository. Edit the version number in the dependency snippet to match your JDBC driver version. For example:

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-jdbc</artifactId>
<version>2.1.0</version>
</dependency>
2

In the pom.xml file for your project, paste the snippet into the dependencies list as follows:

<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-jdbc</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
3

To connect to your federated database instance, create a connection string and open a connection from your application. The connection string for the JDBC driver follows the format of the standard MongoDB connection string, except with the jdbc: prefix:

jdbc:mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?option1=value1[&option2=value2]...]

The following example demonstrates how to open a connection. In addition to the connection string, you must also specify the database to use through a Properties object parameter. To learn more, see Connection Strings and Connection Properties.

java.util.Properties p = new java.util.Properties();
p.setProperty("database", "<databaseName>");
Connection conn = DriverManager.getConnection("<connectionString>", p);

Note

The driver can only connect to Atlas and not to a mongod instance. Any special characters in the connection string for the JDBC driver must be URL encoded.

← MongoDB Shell
ODBC →