0% found this document useful (0 votes)
50 views5 pages

Dbms Assignment 4

This document is an assignment submitted by a student named Prathamesh Nikam for their Database Management Systems laboratory class. The assignment asks the student to write a Java program that implements MongoDB database connectivity and allows performing CRUD operations using JDBC/ODBC. The program code provided connects to a MongoDB database, defines a method to get user input for different operations, and implements adding, updating, deleting and retrieving documents from a collection through a switch case.

Uploaded by

Omkar Thange
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views5 pages

Dbms Assignment 4

This document is an assignment submitted by a student named Prathamesh Nikam for their Database Management Systems laboratory class. The assignment asks the student to write a Java program that implements MongoDB database connectivity and allows performing CRUD operations using JDBC/ODBC. The program code provided connects to a MongoDB database, defines a method to get user input for different operations, and implements adding, updating, deleting and retrieving documents from a collection through a switch case.

Uploaded by

Omkar Thange
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment No.

B-4

Title Write a program to implement MogoDB database connectivity with


Java Implement Database navigation operations (add, delete, edit)
using ODBC/JDBC.
Roll No.
19121003 (Prathamesh Nikam)
Class T.E. (C.E.)
Date 27/11/2021
Subject Database Management System Laboratory
Signature

Assignment No. B4
Title: Write a program to implement MogoDB database connectivity with Java Implement
Database navigation operations (add, delete, edit etc. ) using
ODBC/JDBC.

import java.net.UnknownHostException;
import java.util.Scanner; import
com.mongodb.*;
public class DatabaseConnectivity
{private static void choice_input(){
1.insertdatainto database\n2.updatedatabase
documents\n3.delete database documents\n4.show database
collections\n5.Exit");

public static void main(String[] args) {


String key, value;
Scanner scanner = new
Scanner(System.in); int choice;
try {
Mongo mongo = new Mongo("localhost", 27017);
DB db =
DBCollectioncollection=db.getCollection("dummyColl"); do
{ choice_input();
System.out.println("Enter your choice: t');
choice = scanner.nextlnt(); switch (choice)
{ case l :
BasicDBObject document = new BasicDBObject();
String
ch; do {
System.out.println("Enter key: "); key = scanner.next();
System.out.println("Enter value: ");value =
scanner.next();
document.put(key, value);
System.out.println("Do you want to enter more(y/n)? ");
ch = scanner.next(); } while (!ch.equals("n"));
collection.insert(document);
break;
case 2:
BasicDBObject searchObj = new BasicDBObject();
System.out.println("Enter searched key: 't);

key = scanner.next();
System.out.println("Enter searched value: ");
value = scanner.next(); searchObj .put(key,
value);
BasicDBObject newObj = new BasicDBObject(); System.out.println("Enter
new key: ");

key = scanner.next();
System.out.println("Enter new value: ");

value = scanner.next();

newObj.put(key,value);
collection.update(searchObj, newObj);
break; case 3:
System.out.println("Enter removable key: "); key =
scanner.next();
System.out.println("Enter removable value: ");
value = scanner.next();
BasicDBObject removableObj = new
BasicDBObject(); removableObj.put(key,value);
collection.remove(removableObj); break; case 4:
DBCursor cursorDoc = collection.find(); while
(cursorDoc.hasNext()) {
System.out.println(cursorDoc.next()); break;

case 5:
System.exit(0)
;

break;)

while(choice!
= 6);

) catch (UnknownHostException I MongoException e)


{e.printStackTrace();

Output

l. Insert data into database 2.


Update database documents
3.delete database documents
4. Show database collections
5. Exit
Enter your choice:
1
Enter key:2
Enter value:
harish
Do you want to enter more(y/n)?N l.insert
data into database
2.update database documents
3.delete database documents
4.show database collections
5.Exit
Enter your choice:
2
Enter searched key:
2
Enter searched
value: harish Enter new
key:
1
Enter new value:
Sam
1. insert data into database
2.update database documents
3.delete database documents
4.show database collections
5.Exit
Enter your choice:

_id" : { "$oid" : "5bb453bce4b0283ac9d3205d"} , "l" : "sam }


l.insert data into database 2.
update database documents
3.delete database documents 4.show
database collections
5.ExitEnter your choice:
3

Enter removable key:


3
Enter removable value:
hari
1. insert data into database
2.update database documents
3.delete database documents
4.show database collections
5.Exit
Enter your choice:

_id" : { "$0id" : "5bb453bce4b0283ac9d3205d"} , "l" : "sam")


l.insert data into database 2.
update database documents
3.delete database documents
4.show database collections
5.Exit
Enter your choice: 5

You might also like