• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Unable to connect to Mongodb via Java frontend

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I am trying to insert a record in Mongo DB using the following code.  The error that I get is "Error:(21, 51) java: incompatible types: com.mongodb.DB cannot be converted to com.mongodb.client.MongoDatabase".  Basically it's this line of code "MongoDatabase database = mongoClient.getDB("course");" that's giving the error.  I just want to insert  a record in Mongodb.  I am using Mongodb version 3.2, Java 8, and Intellj Idea as my IDE.  I am not able to get MongoDatabase database = mongoClient.getDatabase("course") method via intellisensef.

Kindly suggest whats going wrong.
 
Marshal
Posts: 74342
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
Thank you for using code tags, but you didn't quite have them right: the [code=java] part goes before the code and the [/code] part after; I corrected it by moving the second part, so doesn't it look better. Also, you appear to have asked the same question twice, so I have deleted the other post.
Don't use com.mongodb as a package name; as you will see from the Java™ Tutorials, that name belongs to somebody else. Try database or something as a package name.

Let's see if we can't find a MongoDB tutorial. Let's try here. It doesn't appear that you have a connectivity problem, but something to do with types which the compiler doesn't like. About ⅓ way down that tutorial, it gives you this line of code:-Compare that with your line 21, which appears to be the location of the error (thank you for supplying a good copy of the error message .) It seems, as the compiler error message says, that you have something which returns a DB reference, so you appear to have declared the database object with the wrong type. Do you know where the Java® API documentation about MongoDB is? That might be very helpful. Does the documentation appear when you hover your mouse over “getDB” on Eclipse?
 
Harsh Saxena
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch
Thank you for using code tags, but you didn't quite have them right: the [code=java] part goes before the code and the [/code] part after; I corrected it by moving the second part, so doesn't it look better. Also, you appear to have asked the same question twice, so I have deleted the other post.
Don't use com.mongodb as a package name; as you will see from the Java™ Tutorials, that name belongs to somebody else. Try database or something as a package name.

Let's see if we can't find a MongoDB tutorial. Let's try here. It doesn't appear that you have a connectivity problem, but something to do with types which the compiler doesn't like. About ⅓ way down that tutorial, it gives you this line of code:-Compare that with your line 21, which appears to be the location of the error (thank you for supplying a good copy of the error message .) It seems, as the compiler error message says, that you have something which returns a DB reference, so you appear to have declared the database object with the wrong type. Do you know where the Java® API documentation about MongoDB is? That might be very helpful. Does the documentation appear when you hover your mouse over “getDB” on Eclipse?



I am new to CodeRanch; still learning the nuances of code ranch.

Here's some more information that I can give you.  I am actually on the Mongo university course "MongoDb for Java developers" and the package name is as directed in the video namely "com.mongodb.  Here's is what I can see when I hover my mouse over getDB()  (see attachments)

I have tried casting to MongoDb but that has not helped.  Following is the error after casting to Mongo DB

"Connected to the target VM, address: '127.0.0.1:49921', transport: 'socket'
Exception in thread "main" java.lang.ClassCastException: com.mongodb.DBApiLayer cannot be cast to com.mongodb.client.MongoDatabase
at com.mongodb.InsertTest.main(InsertTest.java:21)
Disconnected from the target VM, address: '127.0.0.1:49921', transport: 'socket'"

I have posted this problem on the mongodb unversity dicussion as well but I guess as I am running a week behind no one will look at this query.

mongoerror.png
[Thumbnail for mongoerror.png]
mongoerror1.jpg
[Thumbnail for mongoerror1.jpg]
mongoerror3.jpg
[Thumbnail for mongoerror3.jpg]
 
Campbell Ritchie
Marshal
Posts: 74342
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harsh Saxena wrote:. . . . I am actually on the Mongo university course "MongoDb for Java developers" and the package name is as directed in the video namely "com.mongodb.

In which case they are entitled to use the package name themselves. Remember the naming conventions next time

Here's is what I can see when I hover my mouse over getDB(). . .

That is useful information, but you should be able to get the API documentation for the method by hovering your mouse over the method name. Even more useful information if you can find it.

I have tried casting to MongoDb but that has not helped. . . .

No, you cannot cast to incompatible types. You will (I think) have to declare the database as type DB.

I have posted this problem on the mongodb unversity dicussion . . .

That is more useful information for us; please also tell them on that site that you have posted here, to avoid duplication of effort.

I guess as I am running a week behind no one will look at this query.

Don't be so pessimistic about them
 
Harsh Saxena
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Harsh Saxena wrote:. . . . I am actually on the Mongo university course "MongoDb for Java developers" and the package name is as directed in the video namely "com.mongodb.

In which case they are entitled to use the package name themselves. Remember the naming conventions next time

Here's is what I can see when I hover my mouse over getDB(). . .

That is useful information, but you should be able to get the API documentation for the method by hovering your mouse over the method name. Even more useful information if you can find it.

I have tried casting to MongoDb but that has not helped. . . .

No, you cannot cast to incompatible types. You will (I think) have to declare the database as type DB.

I have posted this problem on the mongodb unversity dicussion . . .

That is more useful information for us; please also tell them on that site that you have posted here, to avoid duplication of effort.

I guess as I am running a week behind no one will look at this query.

Don't be so pessimistic about them



Hi,

I could finally resolve this one on my own .  

I update the mongo-java-driver to the latest version of 3.6.3, that gave the option of " mongoClient.getDatabase()" insetad of getDB(); this has sorted out the issue and I am able to insert a record in the database through my Java front end code.

Thanks for your assistance
 
Campbell Ritchie
Marshal
Posts: 74342
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done sorting it out
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic