I am unable to connect to a MongoDB on localhost using JDBC.
The problem is that when trying to connect to MongoDB using JDBC, I get a
java.lang.ClassNotFoundException: mongodb.jdbc.MongoDriver
Error
using
Class.forName("mongodb.jdbc.MongoDriver")
Below are my maven imports.
<dependencies>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-jdbc -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-jdbc</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
Here's the relevant code. It fails on the Connection line.
public static void main(String[] args)
{
try
{
// user = "corpus", pw = "the+password"
Class.forName("com.mongodb.jdbc.MongoDriver");
Connection connection = DriverManager.getConnection(
"jdbc:mongo://localhost:27017/corpus_test",
"corpus", "the+password");
}
catch (SQLException e)
{
System.out.println(e.getMessage());
}
catch (ClassNotFoundException e)
{
throw new RuntimeException(e);
}
}
I've tried commenting out one or both dependencies, making sure both were most current, etc. Also searched high and low online, but can't find a solution.
Would appreciate some guidance here.
Thanks,