4

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.

2
  • have you tried MongoClient? MongoClient mongo = new MongoClient( "localhost" , 27017 );
    – Natalia
    Oct 6, 2015 at 13:15
  • thanks for the reply, I have tried that, but the requirement is to use the jdbc connector.
    – manoj
    Oct 8, 2015 at 10:04

6 Answers 6

3

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.

1
2

If you are getting a ClassNotFoundException, the issue is that the jar containing the mongodb.jdbc.MongoDriver class is not on your classpath. If you're not sure what JAR this class is in, I would reccomend getting 7-Zip so that you can inspect the contents of the jar and see for yourself if the class is there.

The correct way to connect to MongoDB with your approach is:

Class.forName("mongodb.jdbc.MongoDriver");
String URL = "jdbc:mongo://<servername>:<port>/<databaseName>";
Connection jdbcConn = DriverManager.getConnection(url,"user","pass");


But MongoDB isn't really meant to be used with JDBC, so if your requirements allow, I would reccomend getting a connection the "mongodb" way.

MongoClient client = new MongoClient("localhost");

For details on how to do it this way, see the MongoDB docs

1
  • Thanks but I am not getting the mongodb jar file, which has this path and the class MongoDriver. I also checked with Unity-jdbc forum, but there jar files also does not have the same.
    – manoj
    Oct 8, 2015 at 10:09
1

I know its very late to answer but might help someone else. If you are compiling and running your code from cmd then before compilation set classpath for mongo.jar like below :

set classpath=C:\DemoProject\java db\Mongo\mongo.jar;

then run your code.

or if you are using editor like eclipse then add this jar to your lib folder.

0

I met this question today morning. The key is missing mongo-java-driver.jar. when I add the jar, the project can run normal.

0

DbSchema database designer is providing an Open Source MongoDb JDBC driver which does support native MongoDb queries, including find(), projections, aggregate, etc.. The driver is using an internal embedded JavaScript engine. The driver is Open Source on GitHub. Few of the driver features:

  • Support native MongoDb queries
  • Calling DatabaseMetaData methods can 'guess' the collection structure, so a 'virtual schema' is created. This is used by the Designer for MongoDB to represent the MongoDb database structure in diagrams like below.
  • Implement most of the JDBC driver methods. Use the native MongoDB JDBC URL to connect, which means full functionality regarding connectivity.

DbSchema Diagram Design for MongoDB

And one snippet of code about how to use the driver

Class.forName("com.dbschema.MongoDbJdbcDriver");
Properties properties = new Properties();
properties.put("user", "someuser");
properties.put("password", "somepassword" );
Connection con = DriverManager.getConnection("jdbc:mongodb://host1:9160/keyspace1", properties);
// OTHER URL (SAME AS FOR MONGODB NATIVE DRIVER): mongodb://db1.example.net,db2.example.net:2500/?replicaSet=test&connectTimeoutMS=300000
String query = "db.sampleCollection().find()";
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery( query );
Object json = rs.getObject(1);
0

The first option

    MongoClient mongoClient = new MongoClient( "1.2.3.4",27017 );
    MongoDatabase database =  mongoClient.getDatabase(dataBase);
    MongoCollection<Document> collection = database.getCollection(DBcollection);

another option

 MongoClientURI connectionString = new MongoClientURI("mongodb://1.2.3.4:27017");
     MongoClient mongoClient = new MongoClient(connectionString);
    MongoDatabase database =  mongoClient.getDatabase(dataBase);
    MongoCollection collection = database.getCollection(DBcollection);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.