관계형모델

테이블간의 관계형모델이다.

Main


public class Main {

	public static void main(String[] args) {
		Connection CON=Database.CON;
		Scanner s = new Scanner(System.in);
		CampDAO cdao = new CampDAO();
		FacilityDAO fdao = new FacilityDAO();
		StyleDAO sdao =new StyleDAO();
		boolean run =true;
			while(run) {
				System.out.println("");
				System.out.println("┌────────────────────────────────────────┐");
				System.out.print("│1.캠핑장목록  ");
				System.out.print("2. 캠핑장조회  ");
				System.out.print("3.시설물등록 ");
				System.out.print("4.스타일등록  ");
				System.out.print("5.시설물삭제  ");
				System.out.print("6.스타일삭제  ");
				System.out.print("0.프로그램종료   │\n");
				System.out.println("└────────────────────────────────────────┘");
				System.out.print("선택>");
				String menu = s.nextLine();
				switch(menu) {
				case "1": 
					ArrayList<HashMap<String,Object>> carray = cdao.list();
					for(HashMap<String,Object> cmap:carray) {
						System.out.println(cmap.get("cno"));
						System.out.println("캠핑장이름 :"+	cmap.get("cname"));
						System.out.println("전화번호 :"+	cmap.get("ctel"));
						//시설물목록
						String cno = String.valueOf(cmap.get("cno"));
						ArrayList<HashMap<String, Object>> farray = fdao.list(cno);
						System.out.print("시설물:");
						for(HashMap<String, Object> fmap:farray) {
							System.out.print(fmap.get("fname") + "|");
						} 
						System.out.println("");
						//스타일출력
						ArrayList<HashMap<String, Object>> sarray = sdao.list(cno);
						System.out.print("스타일:");
						for(HashMap<String, Object> smap:sarray) {
							System.out.print(smap.get("sname") + "|");
						} 
						System.out.println("");
						System.out.println("──────────────────");
					}
					break;
					
				case "2" :
					System.out.print("캠핑장번호>"); String cno = s.nextLine();
					HashMap<String, Object> map = cdao.read(cno);
					if(map.get("cname") == null){
						System.out.println("해당 캠핑장이 존재하지않습니다!");
					}else {
						System.out.println("캠핑장명:" +map.get("cname"));
						System.out.println("전화번호:"+map.get("ctel"));
						//시설물출력
						ArrayList<HashMap<String, Object>> farray = fdao.list(cno);
						System.out.print("시설물:");
						for(HashMap<String, Object> fmap:farray) {
							System.out.print(fmap.get("fname") + "|");
						} 
						System.out.println("");
						//스타일출력
						ArrayList<HashMap<String, Object>> sarray = sdao.list(cno);
						System.out.print("스타일:");
						for(HashMap<String, Object> smap:sarray) {
							System.out.print(smap.get("sname") + "|");
						} 
						System.out.println("");
					}
					break;
					
				case "3" :
					System.out.print("캠핑장번호>"); cno = s.nextLine();
					map = cdao.read(cno);
					if(map.get("cname") == null){
						System.out.println("해당 캠핑장이 존재하지않습니다!");
					}else {
						System.out.println("캠핑장명:" +map.get("cname"));
						System.out.println("전화번호:"+map.get("ctel"));
						//시설물출력
						ArrayList<HashMap<String, Object>> farray = fdao.list(cno);
						System.out.print("시설물:");
						for(HashMap<String, Object> fmap:farray) {
							System.out.print(fmap.get("fname") + "|");
						} 
						System.out.println("");
						System.out.println("──────────────────");
						//시설물전체목록
						ArrayList<HashMap<String, Object>>  afarray = fdao.list();
						for(HashMap<String, Object> afmap:afarray) {
							System.out.printf("%s:%s|",
									afmap.get("fno"),afmap.get("fname"));
						} 
					}
					System.out.println("");
					System.out.print("시설물 번호>"); String fno = s.nextLine();
					int result =fdao.insert(cno, fno);
					if(result ==0) {
						System.out.println("시설물 번호가 없습니다.");
					}else if(result == 2) {
						System.out.println("이미등록된 시설물입니다!");
					}else {
						System.out.println("시설물등록완료!");
					}
					break;
					
				case "4" :
					System.out.print("캠핑장번호>"); cno = s.nextLine();
					map = cdao.read(cno);
					if(map.get("cname") == null){
						System.out.println("해당 캠핑장이 존재하지않습니다!");
					}else {
						System.out.println("캠핑장명:" +map.get("cname"));
						System.out.println("전화번호:"+map.get("ctel"));
						//스타일출력
						ArrayList<HashMap<String, Object>> sarray = sdao.list(cno);
						System.out.print("스타일:");
						for(HashMap<String, Object> smap:sarray) {
							System.out.print(smap.get("sname") + "|");
						} 
						System.out.println("");
						//스타일전체목록
						ArrayList<HashMap<String, Object>>  asarray = sdao.list();
						for(HashMap<String, Object> asmap:asarray) {
							System.out.printf("%s:%s|",
									asmap.get("sno"),asmap.get("sname"));
						} 
					}
					System.out.println("");
					System.out.print("스타일 번호>"); String sno = s.nextLine();
					result =sdao.insert(cno, sno);
					if(result ==0) {
						System.out.println("스타일 번호가 없습니다.");
					}else if(result == 2) {
						System.out.println("이미등록된 스타일입니다!");
					}else {
						System.out.println("스타일등록완료!");
					}
					break;
				case "5" :
					System.out.print("캠핑장번호>"); cno = s.nextLine();
					map = cdao.read(cno);
					if(map.get("cname") == null){
						System.out.println("해당 캠핑장이 존재하지않습니다!");
					}else {
						System.out.println("캠핑장명:" +map.get("cname"));
						System.out.println("전화번호:"+map.get("ctel"));
					}
					ArrayList<HashMap<String, Object>> farray = fdao.list(cno);
					System.out.print("보유한 시설물:");
					for(HashMap<String, Object> fmap:farray) {
						System.out.print(fmap.get("fname") + "|");
					} 
					System.out.println("");
					System.out.println("──────────────────");
					//시설물전체목록
					ArrayList<HashMap<String, Object>>  afarray = fdao.list();
					for(HashMap<String, Object> afmap:afarray) {
						System.out.printf("%s:%s|",
								afmap.get("fno"),afmap.get("fname"));
						
					} 
					System.out.println("");
					System.out.print("삭제할 시설물 번호 >");
					fno = s.nextLine();
					System.out.print("정말삭제하시겠습니까?(y/Y)?");
					String sel = s.nextLine();
					
					if(sel.equals("Y")||sel.equals("y")) {
						if(fdao.delete(cno,fno)) {
							System.out.println("시설물 삭제 완료!");
						}else {
							System.out.println("시설물 삭제 실패!");
						}
					}
					
					
					
					
					break;
				case "6" :
					System.out.print("캠핑장번호>"); cno = s.nextLine();
					map = cdao.read(cno);
					if(map.get("cname") == null){
						System.out.println("해당 캠핑장이 존재하지않습니다!");
					}else {
						System.out.println("캠핑장명:" +map.get("cname"));
						System.out.println("전화번호:"+map.get("ctel"));
						//스타일출력
						ArrayList<HashMap<String, Object>> sarray = sdao.list(cno);
						System.out.print("스타일:");
						for(HashMap<String, Object> smap:sarray) {
							System.out.print(smap.get("sname") + "|");
						} 
						System.out.println("");
					}
						
					System.out.println("──────────────────");
					//스타일 전체목록
					ArrayList<HashMap<String, Object>>  asarray = sdao.list();
					for(HashMap<String, Object> asmap:asarray) {
						System.out.printf("%s:%s|",
								asmap.get("sno"),asmap.get("sname"));
					} 
					System.out.println("");
					System.out.print("삭제할 스타일 번호 >");
					sno = s.nextLine();
					System.out.print("정말삭제하시겠습니까?(y/Y)?");
					sel = s.nextLine();
					if(sel.equals("Y")||sel.equals("y")) {
						if(sdao.delete(cno,sno)) {
							System.out.println("스타일 삭제 완료!");
						}else {
							System.out.println("스타일 삭제 실패!");
						}
					}
					break;
				case "0" :
					run=false;

					break;
				default:
					System.out.println("0~6번 메뉴를 선택하세요!");
				}

			}
			System.out.println("프로그램종료!");
	}
}

CampDAO

public class CampDAO {
	//캠핑장 조회
	public HashMap<String, Object> read(String cno){
		HashMap<String, Object> map = new HashMap<>();
		try {
			String sql = "select * from tbl_camp where cno=?";
			PreparedStatement ps = Database.CON.prepareStatement(sql);
			ps.setString(1, cno);
			ResultSet rs = ps.executeQuery();
			if(rs.next()) {
				map.put("cno", rs.getString("cno"));
				map.put("cname", rs.getString("cname"));
				map.put("ctel", rs.getString("ctel"));
				
			}
			
		}catch(Exception e) {
			System.out.println("");
		}
		return map;
		
	}
	
	//캠핑장 목록
	public ArrayList<HashMap<String,Object>> list() {
		ArrayList<HashMap<String,Object>> array = new ArrayList<>(); 
		try {
			String sql="select * from tbl_camp";
			PreparedStatement ps = Database.CON.prepareStatement(sql);
			ResultSet rs = ps.executeQuery();
			while(rs.next()) {
				HashMap<String, Object> map = new HashMap<>();
				map.put("cno", rs.getInt("cno"));
				map.put("cname", rs.getString("cname"));
				map.put("ctel", rs.getString("ctel"));
				array.add(map);
			}
		}catch(Exception e) {
			System.out.println("camp list:"+e.toString());
		}
		return array;
	}
}

FacilityDAO


	
	// 특정 캠핑장의 시설물 목록
	public ArrayList<HashMap<String, Object>> list(String cno) {
		 ArrayList<HashMap<String, Object>> array = new ArrayList<>();
		 try {
			 String sql = "select * from view_camp_facility where cno=?";
			 PreparedStatement ps = Database.CON.prepareStatement(sql);
			 ps.setString(1, cno);
			 ResultSet rs=ps.executeQuery();
			 while(rs.next()) {
				HashMap<String, Object> map = new HashMap<>();
				map.put("fno", rs.getString("fno"));
				map.put("fname", rs.getString("fname"));
				array.add(map);
			 }
			 
		 }catch(Exception e) {
			 
		 }
		return array;
	}
	
	//전체시설물목록
	public ArrayList<HashMap<String, Object>> list() {
		 ArrayList<HashMap<String, Object>> array = new ArrayList<>();
		 try {
			 String sql = "select * from tbl_facility";
			 PreparedStatement ps = Database.CON.prepareStatement(sql);
			 ResultSet rs=ps.executeQuery();
			 while(rs.next()) {
				HashMap<String, Object> map = new HashMap<>();
				map.put("fno", rs.getString("fno"));
				map.put("fname", rs.getString("fname"));
				array.add(map);
			 }
			 
		 }catch(Exception e) {
		 }
		return array;
	}
	
	//시설물 등록
	public int insert(String cno, String fno) {
		int result = 0;
		try {
			String sql = "call add_camp_facility(?,?,?,?)";
			CallableStatement cs = Database.CON.prepareCall(sql);
			cs.setInt(1, Integer.parseInt(cno));
			cs.setInt(2, Integer.parseInt(fno));
			cs.registerOutParameter(3, oracle.jdbc.OracleTypes.INTEGER);
			cs.registerOutParameter(4, oracle.jdbc.OracleTypes.INTEGER);
			cs.execute();
			int c_count = (int)cs.getObject(3);
			int f_count = (int)cs.getObject(4);
			if(c_count==0) {
				result = 0;  //시설물 테이블에 시설물이 없는경우
			}else if (f_count==1) {
				result=2;   //캠핑장 시설물에 시설물이 이미 등록된 경우
			}else {
				result =1;  // 시설물 등록 성공
			}
		}catch(Exception e){
			
		}
		return result;
	}

	//시설물삭제
	public boolean delete(String cno,String fno) {
		boolean success = false;
		try {
			String sql = "select * from tbl_camp_facility where cno =? and fno =?";
			PreparedStatement ps = Database.CON.prepareStatement(sql);
			 ps.setString(1, cno);
			 ps.setString(2, fno);
			 ResultSet rs=ps.executeQuery();
			 if(rs.next()) {
				 sql = "delete from tbl_camp_facility where cno =? and fno =?";
				 ps = Database.CON.prepareStatement(sql);
				 ps.setString(1, cno);
				 ps.setString(2, fno);
				 ps.execute();
				 success=true;
			 }
	}catch(Exception e){
		
	}
		return success;	
	}
}

StyleDAO

public class StyleDAO {
	
	// 특정 캠핑장의 스타일 목록
	public ArrayList<HashMap<String, Object>> list(String cno) {
		 ArrayList<HashMap<String, Object>> array = new ArrayList<>();
		 try {
			 String sql = "select * from view_camp_style where cno=?";
			 PreparedStatement ps = Database.CON.prepareStatement(sql);
			 ps.setString(1, cno);
			 ResultSet rs=ps.executeQuery();
			 while(rs.next()) {
				HashMap<String, Object> map = new HashMap<>();
				map.put("sno", rs.getString("sno"));
				map.put("sname", rs.getString("sname"));
				array.add(map);
			 }
			 
		 }catch(Exception e) {
			 
		 }
		return array;
	}
	
	//스타일 전체 리스트
	public ArrayList<HashMap<String, Object>> list() {
		 ArrayList<HashMap<String, Object>> array = new ArrayList<>();
		 try {
			 String sql = "select * from tbl_style";
			 PreparedStatement ps = Database.CON.prepareStatement(sql);
			 ResultSet rs=ps.executeQuery();
			 while(rs.next()) {
				HashMap<String, Object> map = new HashMap<>();
				map.put("sno", rs.getString("sno"));
				map.put("sname", rs.getString("sname"));
				array.add(map);
			 }
			 
		 }catch(Exception e) {
			 
		 }
		return array;
	}

	//스타일 등록
	public int insert(String cno, String sno) {
		int result = 0;
		try {
			String sql = "call add_camp_style(?,?,?,?)";
			CallableStatement cs = Database.CON.prepareCall(sql);
			cs.setInt(1, Integer.parseInt(cno));
			cs.setInt(2, Integer.parseInt(sno));
			cs.registerOutParameter(3, oracle.jdbc.OracleTypes.INTEGER);
			cs.registerOutParameter(4, oracle.jdbc.OracleTypes.INTEGER);
			cs.execute();
			int c_count = (int)cs.getObject(3);
			int s_count = (int)cs.getObject(4);
			if(c_count==0) {
				result = 0;  //시설물 테이블에 시설물이 없는경우
			}else if (s_count==1) {
				result=2;   //캠핑장 시설물에 시설물이 이미 등록된 경우
			}else {
				result =1;  // 시설물 등록 성공
			}
		}catch(Exception e){
			
		}
		return result;
	}
	
	//스타일 삭제
	public boolean delete(String cno,String sno) {
		boolean success = false;
		try {
			String sql = "select * from tbl_camp_style where cno =? and sno =?";
			PreparedStatement ps = Database.CON.prepareStatement(sql);
			 ps.setString(1, cno);
			 ps.setString(2, sno);
			 ResultSet rs=ps.executeQuery();
			 if(rs.next()) {
				 sql = "delete from tbl_camp_style where cno =? and sno =?";
				 ps = Database.CON.prepareStatement(sql);
				 ps.setString(1, cno);
				 ps.setString(2, sno);
				 ps.execute();
				 success=true;
			 }
	}catch(Exception e){
		
	}
		return success;
	}
}

추가된 SQL 문


select count(*) f_count from tbl_facility where fno=5;
select count(*) c_count from tbl_camp_facility where cno = 1 and fno=2;

--------------------------------------------

variable f_count number;
variable c_count number;

exec add_camp_facility(3, 4, :f_count, :c_count);
print f_count;
print c_count;

select * from tbl_camp_facility;

create view view_camp_facility as
(select c.*, f.fname
from tbl_camp_facility c, tbl_facility f
where c.fno= f.fno );

select * from view_camp_facility;
------------------------------------------

variable s_count number;
variable c_count number;

exec add_camp_style(3, 3, :s_count, :c_count);

select * from tbl_camp_style;
print s_count;
print c_count;

create view view_camp_style as
(select c.*, sname
from tbl_camp_style c, tbl_style s
where c.sno= s.sno) ;

select * from view_camp_style;

프로시저란걸 이용해서 만들었다.

ADD_CAMP_FACILITY

CREATE OR REPLACE PROCEDURE ADD_CAMP_FACILITY (
i_cno in int,
i_fno in int,
f_count out int, -- 시설물정보 테이블에 있는지 1 없으면 0
c_count out int  -- 캠핑장 시설물 테이블에 있는지 1 없으면 0
)
AS 
BEGIN
  select count(*) into f_count from tbl_facility where fno=i_fno;
  select count(*) into c_count from tbl_camp_facility where cno=i_cno and fno=i_fno;

  if(f_count=1 And c_count=0) then  -- 시설물이 있으면서 캠핑장 시설물테이블에는 없음
    insert into tbl_camp_facility(cno,fno) values(i_cno,i_fno);
  end if;
    
END ADD_CAMP_FACILITY;

ADD_CAMP_STYLE

CREATE OR REPLACE PROCEDURE ADD_CAMP_STYLE (

i_cno in int,
i_sno in int,
s_count out int, 
c_count out int 
)
AS 
BEGIN
  select count(*) into s_count from tbl_style where sno=i_sno;
  select count(*) into c_count from tbl_camp_style where cno=i_cno and sno=i_sno;

  if(s_count=1 And c_count=0) then 
    insert into tbl_camp_style(cno,sno) values(i_cno,i_sno);
  end if;
  
END ADD_CAMP_STYLE;

프로시저란 ?

프로세스를 절차적으로 기술해 놓은것

특정한 로직을 처리하기만 하고 결과 값을 반환하지 않는 서브 프로그램이다. 테이블에서 데이터를 추출해 조작하고 그 결과를 다른 테이블에 다시 저장하거나 갱신하는 일련의 처리를 할 때 주로 프로시저를 사용한다.

태그:

카테고리:

업데이트:

댓글남기기