0

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,

Morkus
  • 517
  • 7
  • 21
  • It's `com.mongodb.jdbc.MongoDriver` not `mongodb.jdbc.MongoDriver`. You are missing the `com.` at the start. – andrewJames Jul 08 '22 at 16:12
  • Also, it's worth noting that you may not even need that `Class.forName` statement (depending on the specific circumstances in which you are using the driver) since the driver can be loaded automatically via [SPI](https://www.baeldung.com/java-spi) without needing `Class.forName`. – andrewJames Jul 08 '22 at 16:19
  • The "com." in front did not help, unfortunately. I still get: "No suitable driver found for mongodb". – Morkus Jul 08 '22 at 17:06
  • Understood. Can you show us your code? A [mre] and the full error message. – andrewJames Jul 08 '22 at 17:23
  • Added to code above... – Morkus Jul 08 '22 at 17:26
  • The `Class.forName()` command is no longer throwing an error - your latest error message (_no suitable driver found_) is different from the original one (_ClassNotFoundException_). Double-check your connection URL and make sure the database is available. See also [The infamous java.sql.SQLException: No suitable driver found](https://stackoverflow.com/q/1911253/12567365) for more background notes. – andrewJames Jul 08 '22 at 19:10
  • Thanks. The database is up and working on localhost. How would I check the connection URL? – Morkus Jul 08 '22 at 20:08
  • 1
    According to the [official documentation](https://github.com/mongodb/mongo-jdbc-driver), the URL should start with `jdbc:mongodb://` - but you have something different: `jdbc:mongo://`. So take a look at the docs and try that change. – andrewJames Jul 08 '22 at 20:27
  • 1
    I appreciate all your incredible help and support! Thank you! My issue now is that I'm getting the infamous error 18 on authentication. I'm using the same connection string that is built in Mongo Compass which lets me log in. Maybe JDBC isn't that great an approach anyway with a non relational DB anyway? – Morkus Jul 09 '22 at 12:11
  • 1
    Agreed. Unless you have a reason why you must use the JDBC connector, I say you should use the native connector instead. (Your latest problem is really a new problem - and maybe if you really need an answer, you should ask a new question with full details specific to that new problem. I have never used the JDBC connector for Mongo - except for a DB with no credentials, so I have not encountered your specific problem.) – andrewJames Jul 09 '22 at 14:14

0 Answers0