java


Connection to MongoDB with Java


I am trying to connect my java project to an online mongo database. I faced some issues so I decided to first try to connect on a local mongo DB but I still have the same problem.
I am using the 3 mongodb libraries (driver,driver-async and driver-core), 3.3.0 version and the bson library, 3.2.2 version.
When I launch my 'helloworld example' , copied from here : http://www.mkyong.com/mongodb/java-mongodb-hello-world-example/
I have the following errors :
Exception in thread "main" java.lang.NoClassDefFoundError: org/bson/codecs/IterableCodecProvider
at com.mongodb.MongoClient.<clinit>(MongoClient.java:84)
at mainClass.main(mainClass.java:24)
Caused by: java.lang.ClassNotFoundException: org.bson.codecs.IterableCodecProvider
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
Note : The example code uses a method which is now deprecated, but even by replacing the code with the new version of it (using MongoClient), I still have the same errors.
Solution :
Download the last version of BSON on :
https://oss.sonatype.org/content/repositories/releases/org/mongodb/bson/
or
https://mvnrepository.com/artifact/org.mongodb/bson/
The code furnished in the example need to be updated : use MongoClient instead of just Mongo. Insert example code :
/**** Connect to MongoDB ****/
String uri = <URI of your mongo DB>
MongoClientURI mgURI = new MongoClientURI(uri);
MongoClient mongo = new MongoClient(mgURI);
/**** Get database ****/
MongoDatabase dataBase = mongo.getDatabase("databaseName");
MongoCollection<Document> userCollection = dataBase.getCollection("CollectionName");
Document document = new Document();
document.put("key1", value1);
document.put("key2", "value2");
// insert into database
userCollection.insertOne(document);
Thanks to Veeram for his answers :)

Related Links

JUnit testing System.err.print()
JPA issue mapping file issue
OAuth verification failed with seatseller api
Can not access SharedPreferences value in another class after restart
ANdroid Studio >> GetCount()
Two ScheduledExecutorService overriding eachother?
Possible to disable Java autoboxing?
How to go a specific position?
How to improve java nio2 net performance?
Copying a variable number of methods from one place to another in Java
How to combine the KeyUpHandler and the ValueChangeHandler in GWT?
How to disable a button while processing
Tracking multiple presses via libgdx and android
Little Confusion Over Hashing
Multiple buttons with different layouts
Listview with pull to refersh Scroll down to last item when adding header-view

Categories

HOME
ibm-bluemix
pandas
gerrit
deezer
mfc
relative-path
objectgears
cplex
routes
sqlite-net-extensions
jira
programming-languages
cakephp-2.5
umd
disassembler
windows-server
paradox
multiple-records
gorm
reveal.js
midi
kentor-authservices
wheelnav.js
zebra-printers
decomposition
vaadin7
normalizr
jqwidget
lldb
jtextfield
kryo
scriptcs
tokenize
arabic
bootstrap-tour
microsoft-sync-framework
intel-pin
nhapi
entitlements
ejabberd-module
r-raster
fabric8
google-qpx-express-api
kendo-ui-grid
repo
accelerate-framework
mpmediaquery
wpf-controls
http-digest
leading-zero
unoconv
galen
ruby-on-rails-3.1
gulp-sourcemaps
no-www
turbogears
executenonquery
python-webbrowser
dotnetzip
brightcove
vtigercrm
topbeat
messenger
angular-strap
libpng
home-directory
database-optimization
disque
lua-5.1
yaws
bluemix-app-scan
dataview
vhd
separator
jms2
jqgrid-formatter
operation
codeigniter-routing
html-helper
has-many-through
uitouch
jquery-layout
undefined-reference
app42
mysqltuner
blending
dbconnection
jquery-mobile-dialog
unc
cisco-jtapi
bluepill
ocx
reddot
postgresql-performance
celltable
actionview
getresponsestream
ncqrs
multi-tier
project-hosting
grid-system
multiple-languages

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App