Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (50.2k points)

I want to connect my MongoDB remote server with the help of java. My code is as follows:

    URL = "jdbc:mongo://" + serverIP + ":"

    + port+ "/" +databaseName;                     

    Class.forName("mongodb.jdbc.MongoDriver");

    dbConn = getConnection(URL,mongo1, mongo1);

I have also implemented the Unity_trial.Jar, mongo_version.jar files but the error displays as:

 'mongodb.jdbc.MongoDriver' classNameNotFound.

If I add class.forname as the comment then this will display the below error:

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

    + "/" +databaseName;

which is not in the correct format. 

I am not able to understand this. Can you help me?

1 Answer

0 votes
by (107k points)
edited by

You have to register the driver before using JDBC with MongoDB. For registration, you can use the Class.forName function, refer the following code:

try {

  Class.forName("mongodb.jdbc.MongoDriver");

} catch (ClassNotFoundException e) {

  System.out.println("ERROR: Unable to load SQLServer JDBC Driver");

  e.printStackTrace();

  return;

}

You have to keep one thing in mind that in Java 6, the registration process of the JDBC Drivers is no longer required and you can eliminate that. Even if you are registering, it will not cause any harm but it will definitely provide you backward compatibility with older JDKs.

Want to learn MongoDB from the experts, Check out this MongoDB Training in United States to enhance your Knowledge!

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...