0% found this document useful (0 votes)
5 views3 pages

Assignment B4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

Assignment B4

Problem Statement: Database Connectivity:


Write a program to implement MongoDB database connectivity with any front
end language to implement Database navigation operations (add, delete, edit
etc.)

Code:
import java.util.Scanner;
import com.mongodb.*;
public class conn
{
public static void main(String[] args)
{
Try
{
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB( "mydb" );
DBCollectipackage conMongo1;
on coll = db.createCollection("Stud",null);
int ch = 0;
do
{
System.out.println("Enter your choice: \n 1.Insert \n2.Select \n3.Update \n4.Delete");
Scanner s=new Scanner(System.in);
ch=s.nextInt();

switch (ch)
{
case 1:

BasicDBObject doc1 = new


BasicDBObject("age","1").append("name","Mona");
BasicDBObject doc2 = new
BasicDBObject("age","2").append("name","swati");
coll.insert(doc1);
coll.insert(doc2);
DBCursor cursor = coll.find();
System.out.println("Inserted into db");
while(cursor.hasNext())
{
System.out.println(cursor.next());
}
break;
case 2:
DBCursor cursor3 = coll.find();
System.out.println("Display Records");
while(cursor3.hasNext())
{
System.out.println(cursor3.next());
}
break;
case 3:
BasicDBObject newDocument = new BasicDBObject();
newDocument.append("$set", new BasicDBObject().append("name",
"Mohan"));
newDocument.append("$set", new BasicDBObject().append("name",
"rahul"));
BasicDBObject searchQuery = new BasicDBObject().append("name",
"Ashwini");
BasicDBObject searchQuery1 = new BasicDBObject().append("name",
"swati");

coll.update(searchQuery, newDocument);
coll.update(searchQuery1, newDocument);
DBCursor cursor2 = coll.find();
System.out.println("Updated into db");

while(cursor2.hasNext())
{
System.out.println(cursor2.next());
}
break;
case 4:
System.out.println("Data is removed");
BasicDBObject R1 = new BasicDBObject();
R1.put("name", "Mona");
coll.remove(R1);
BasicDBObject R2 = new BasicDBObject();
R1.put("name", "swati");
coll.remove(R2);
break;
default:
break;
}

} while (ch!=6);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Output:
Enter your choice:
1.Insert
2.Select
3.Update
4.Delete
1
Inserted into db
{ "_id" : { "$oid" : "59df071e24c86b873e72d2de"} , "age" : "1" , "name" : "Ashwini"}
{ "_id" : { "$oid" : "59df071e24c86b873e72d2df"} , "age" : "2" , "name" : "swati"}
Enter your choice:
1.Insert
2.Select
3.Update
4.Delete
2
Display Records
{ "_id" : { "$oid" : "59df071e24c86b873e72d2de"} , "age" : "1" , "name" : "Ashwini"}
{ "_id" : { "$oid" : "59df071e24c86b873e72d2df"} , "age" : "2" , "name" : "swati"}
Enter your choice:
1.Insert
2.Select
3.Update
4.Delete
3
Updated into db
{ "_id" : { "$oid" : "59df071e24c86b873e72d2de"} , "age" : "1" , "name" : "Mohan"}
{ "_id" : { "$oid" : "59df071e24c86b873e72d2df"} , "age" : "2" , "name" : "rahul"}
Enter your choice:
1.Insert
2.Select
3.Update
4.Delete
2
Display Records
{ "_id" : { "$oid" : "59df071e24c86b873e72d2de"} , "age" : "1" , "name" : "Mohan"}
{ "_id" : { "$oid" : "59df071e24c86b873e72d2df"} , "age" : "2" , "name" : "rahul"}
Enter your choice:
1.Insert
2.Select
3.Update
4.Delete
4
Data is removed
Enter your choice:
1.Insert
2.Select
3.Update
4.Delete

You might also like