반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

비전공개미 개발노트

Java - 몽고db연결하여 사원테이블 입력, 출력, 삭제, 수정 본문

프로그래밍/Java

Java - 몽고db연결하여 사원테이블 입력, 출력, 삭제, 수정

비전공개미 2022. 9. 22. 19:26
반응형
SMALL
package net.bit.day22;

import org.bson.Document;
import org.bson.conversions.Bson;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Updates;
import static com.mongodb.client.model.Filters.eq;

import java.util.Iterator;
import java.util.Scanner;

public class EmpMongo{
	
	public static void main(String[] args) {
		try {
			String db="test";
			String table="emp"; //empno,ename,dname,hiredate,pay
			MongoClient  mongoclient = null ;
			MongoDatabase mongodb = null ;
			MongoCollection<Document> collection = null ;
			
			//mongoclient = new MongoClient(ip, port);
			mongoclient = MongoClients.create("mongodb://localhost:27017");
			mongodb =mongoclient .getDatabase(db);
			collection = mongodb.getCollection(table);
			
			Scanner sc = new Scanner(System.in);
			try{
				Thread.sleep(500);
			}catch(Exception ex) { }
			System.out.print("1.insert  2.select  3.delete  4.update > ");
			int num = sc.nextInt();
			switch(num) {
			case 1:
				insertOne(collection);
				System.out.println("----------데이터출력----------");
				selectAll(collection);
				break;
			case 2:
				selectAll(collection);
				break;
			case 3:
				deleteOne(collection);
				System.out.println("----------데이터출력----------");
				selectAll(collection);
				break;
			case 4:
				updateOne(collection);
				System.out.println("----------데이터출력----------");
				selectAll(collection);
				break;
			}
				
//			System.out.println("----------데이터출력----------");
//			try{ Thread.sleep(200); }catch(Exception ex) { }
//			selectAll(collection);
			mongoclient.close();
			sc.close();
			
		}catch(Exception ex) {
			System.out.println("에러이유 " +ex);
		}
		
	}//end
	
	public static void insertOne(MongoCollection<Document> collection) {
		Scanner sc = new Scanner(System.in);
		//저자입력>>> 이상한 안내문 뜨는데 그냥 기술하면 되요
//		try{ Thread.sleep(1000); }catch(Exception ex) { }
		
		System.out.print("사원번호입력>>>"); String a=sc.nextLine();
		System.out.print("사원이름입력>>>"); String b=sc.nextLine();
		System.out.print("부서명입력>>>"); String c=sc.nextLine();
		System.out.print("입사일입력>>>"); String d=sc.nextLine();
		System.out.print("연봉입력>>>"); String e=sc.nextLine();
		Document doc = new Document() ;
		doc.append("empno", a);
		doc.append("ename", b);
		doc.append("dname", c);
		doc.append("hiredate", d);
		doc.append("pay", e);
		collection.insertOne(doc);
		System.out.println("몽고디비 emp컬렉션 저장성공");
		sc.close();
	}//end
	
	public static void selectAll(MongoCollection<Document> collection) {
		FindIterable<Document> cursor = collection.find();
		//while문과 동일하게 출력
//		for(Document doc :cursor) {
//			System.out.println(doc.toJson());
//		}
		
		System.out.println();
		Iterator itr = cursor.iterator();
		while(itr.hasNext()==true) {
			System.out.println(itr.next());
		}
		
		System.out.println("몽고디비 emp컬렉션 조회성공 ");
	}//end
	
	
	public static void updateOne(MongoCollection<Document> collection) {
		Scanner sc = new Scanner(System.in);
		
		System.out.print("수정할 사원명 입력 > ");
		String name = sc.nextLine(); 
		Bson query = eq("ename", name); //미리서 저자의 lee입력후 삭제
		
		System.out.print("수정할 부서명 입력 > ");
		String dname = sc.nextLine();
		System.out.print("수정할 연봉 입력 > ");
		String pay = sc.nextLine();
		Bson edit1 = Updates.combine(Updates.set("dname", dname));
		Bson edit2 = Updates.combine(Updates.set("pay", pay));
		collection.updateOne(query,edit1);
		collection.updateOne(query,edit2);
		System.out.println("몽고디비 emp컬렉션 수정성공 ");
		sc.close();
	}//end
	
	public static void deleteOne(MongoCollection<Document> collection) {
		Scanner sc = new Scanner(System.in);
		System.out.print("삭제할 사원명 입력 > ");
		String name = sc.nextLine(); 
		
		collection.deleteOne(eq("ename", name));  //미리서 저자의 java입력후 삭제 
		System.out.println("몽고디비 emp컬렉션 삭제성공 ");
		sc.close();
	}//end
	
}//class END

 

반응형
LIST
Comments