Connecting to MongoDB using jdbc driver

manoj picture manoj · Oct 6, 2015 · Viewed 28.3k times · Source

Purpose is to connect MongoDB remote server through JAVA:

    URL = "jdbc:mongo://" + serverIP + ":"
    + port+ "/" +databaseName;                     
    Class.forName("mongodb.jdbc.MongoDriver");
    dbConn = getConnection(URL,mongo1, mongo1);

Tried Unity_trial.Jar, mongo_version.jar files but the error comes is 'mongodb.jdbc.MongoDriver' classNameNotFound.

If I comment the class.forname line, the next error is

   URL = "jdbc:mongo://" + serverIP + ":" + port
    + "/" +databaseName;

is not in correct format. Not sure about where I am making the mistake. Thanks for your help in advance.

Answer

s.meissner picture s.meissner · Oct 6, 2015

You can checkout this project:

https://github.com/erh/mongo-jdbc

There are two examples given.

But in general I would recommend to use the MongoDB Client or some Spring Data abstraction.