In this post, we take a look at basic CRUD (Create Read Update Delete) operations on MongoDB using Java JDBC.
We only use Eclipse IDE.
To find out more about NoSQL databases, look here.
MongoDB is a scalable, high-performance open source, schema free, Document-oriented NoSQL database and is highly suitable for Web Applications.
To find out more on MongoDB, refer its home site here.
The code which performs all the CRUD operations is as follows.
The screen shot of the output is as follows:
This code is available for download along with the entire project directory here.
We only use Eclipse IDE.
To find out more about NoSQL databases, look here.
MongoDB is a scalable, high-performance open source, schema free, Document-oriented NoSQL database and is highly suitable for Web Applications.
To find out more on MongoDB, refer its home site here.
- Download and Install MongoDB from here.
- Extract the files.
- Run the mongod.exe file present in the bin folder of the Mongodb folder to start the server.
- If you want to open the UI for the database, run the mongo.exe file present in the bin folder.
- You will be needing a mongo.jar file to connect Java to the MongoDB which can be downloaded from here.
- This jar file is also available in the Project Dump which is available for download below. It is present in the folder titled 'JarFiles' in the Project Directory.
- Be sure to add this jar file to the class path.
The code which performs all the CRUD operations is as follows.
package com.examples.mongodb; import com.mongodb.BasicDBObject; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.Mongo ; public class MongoDBExample { public static void main(String[] args) { try { // Mongodb Database Setup Mongo m = new Mongo("localhost", 27017) ; DB db = m.getDB("authDB") ; DBCollection col = db.getCollection("authCollection") ; // Creating Data // setup and insert data into the authDB database BasicDBObject doc = new BasicDBObject() ; doc.put("username", "admin") ; doc.put("password", "admin") ; System.out.println(doc); col.insert(doc) ; // 5 records are inserted in the following 'for' loop for (int i = 0 ; i < 5 ; i++) { BasicDBObject data = new BasicDBObject() ; data.put("username", "user_" + i ) ; data.put("password", "pass_" + i ) ; System.out.println(data); col.insert(data) ; data.clear() ; } System.out.println("Data Inserted"); // Retrieving Data System.out.println("Data Retrieved"); DBCursor cursor = col.find() ; while (cursor.hasNext()) { // The entire records in the collection is displayed here. System.out.println(cursor.next()); } // Updating Data BasicDBObject oldData = new BasicDBObject() ; oldData.put("username", "admin") ; oldData.put("password", "admin") ; BasicDBObject newData = new BasicDBObject() ; newData.put("username", "administrator") ; newData.put("password", "administrator") ; col.update(oldData, newData) ; System.out.println("Data Updated"); DBCursor cursor1 = col.find() ; while (cursor1.hasNext()) { // The updated record with other records present in the collection is displayed here. System.out.println(cursor1.next()); } // Deleting Data // This deletes the record where username equals user_5 BasicDBObject delRecord = new BasicDBObject() ; delRecord.put("username", "user_5") ; col.remove(delRecord) ; System.out.println("Record Deleted") ; DBCursor cursor2 = col.find() ; while (cursor2.hasNext()) { // Here there is no record with username data equals user_5 System.out.println(cursor2.next()); } // Dropping or deleting entire database m.dropDatabase("authDB") ; System.out.println("Database deleted/dropped"); DBCursor cursor3 = col.find() ; while (cursor3.hasNext()) { //Nothing is displayed here as the database is deleted System.out.println(cursor3.next()); } } catch (Exception e) { // TODO: handle exception System.out.println(e.getMessage()) ; e.printStackTrace() ; } } }
The screen shot of the output is as follows:
This code is available for download along with the entire project directory here.
No comments:
Post a Comment