DB- 실습05
- 캠핑장관리테이블
번호 | 캠핑장명 | 연락처 |
---|---|---|
100 | 도토리캠핑장 | 010-1234-5678 |
200 | 다람쥐캠핑장 | 010-5678-1234 |
300 | 솔밭캠핑장 | 010-2314-1234 |
- 시설물테이블
번호 | 이름 |
---|---|
11 | 전기 |
12 | 수도 |
13 | 화장실 |
14 | 수영장 |
- 캠핑장종류테이블
번호 | 이름 |
---|---|
1 | 카라반 |
2 | 오토캠핑 |
3 | 글램핑 |
- 캠핑장시설물테이블
캠핑장번호 | 시설물번호 |
---|---|
100 | 11 |
100 | 12 |
100 | 13 |
200 | 12 |
200 | 13 |
200 | 14 |
300 | 11 |
300 | 12 |
- 캠핑장별 종류테이블
캠핑장번호 | 종류 | 수량 | 일반가격 |
---|---|---|---|
100 | 1 | 10 | 100000 |
100 | 3 | 15 | 50000 |
200 | 2 | 20 | 30000 |
200 | 1 | 10 | 150000 |
300 | 2 | 30 | 50000 |
위 표를 토대로 sql 문을 작성했다.
SQL
-- 캠핑장 테이블
create table tbl_camp (
cno int, --증가시키위해서 int
cname varchar(100) not null, --꼭입력하고싶으면 not null
ctel varchar(20),
primary key(cno)
);
--시설물
create table tbl_facility (
fno int,
fname varchar(100),
primary key(fno)
);
--캠핑장 종류
create table tbl_style (
sno int,
sname varchar(100),
primary key(sno)
);
--캠핑장시설물
create table tbl_camp_facility(
cno int,
fno int,
primary key(cno,fno),
foreign key(cno) references tbl_camp(cno),
foreign key(fno) references tbl_facility(fno)
);
--캠핑장별 종류
create table tbl_camp_style(
cno int,
sno int,
qut int,
price int,
primary key(cno,sno),
foreign key(cno) references tbl_camp(cno),
foreign key(sno) references tbl_style(sno)
);
--테이블제거
drop table tbl_camp_style;
drop table tbl_camp_facility;
drop table tbl_style;
drop table tbl_facility;
drop table tbl_camp;
--캠핑장테이블 테이터입력
insert into tbl_camp(cno, cname,ctel)
values(100,'도토리캠핑장', '010-1234-4556');
insert into tbl_camp(cno, cname,ctel)
values(200,'다람쥐캠핑장', '010-4565-2414');
update tbl_camp set cname='다람쥐캠핑장' where cno=200;
insert into tbl_camp(cno, cname,ctel)
values(300,'솔밭캠핑장', '010-7656-2525');
select * from tbl_camp;
--시설물 데이터입력
insert into tbl_facility(fno,fname)
values(11,'전기');
insert into tbl_facility(fno,fname)
values(12,'수도');
update tbl_facility set fname='수도' where fname=12;
insert into tbl_facility(fno,fname)
values(13,'화장실');
insert into tbl_facility(fno,fname)
values(14,'수영장');
select * from tbl_facility;
select * from tbl_facility where cno=100;
--캠핑장종류 데이터입력
insert into tbl_style(sno,sname)
values(1, '카라반');
insert into tbl_style(sno,sname)
values(2, '오토캠핑');
insert into tbl_style(sno,sname)
values(3, '글램핑');
select * from tbl_style;
select * from tbl_style ;
--캠핑장시설물 데이터입력
insert into tbl_camp_facility(cno,fno)
values(100,11);
insert into tbl_camp_facility(cno,fno)
values(100,12);
insert into tbl_camp_facility(cno,fno)
values(100,13);
insert into tbl_camp_facility(cno,fno)
values(200,12);
insert into tbl_camp_facility(cno,fno)
values(200,13);
insert into tbl_camp_facility(cno,fno)
values(200,14);
insert into tbl_camp_facility(cno,fno)
values(300,11);
insert into tbl_camp_facility(cno,fno)
values(300,12);
select * from tbl_camp_facility;
--캠핑장별 종류 데이터 입력
insert into tbl_camp_style(cno,sno,qut,price)
values(100,1,10,100000);
insert into tbl_camp_style(cno,sno,qut,price)
values(100,3,15,50000);
insert into tbl_camp_style(cno,sno,qut,price)
values(200,2,20,30000);
insert into tbl_camp_style(cno,sno,qut,price)
values(300,1,10,150000);
insert into tbl_camp_style(cno,sno,qut,price)
values(300,2,30,50000);
select * from tbl_camp_style;
--캠핑장별 캠핑장번호, 캠핑장이름,전화번호,시설물 번호, 시설물명
create view view_camp_facility as
(select cf.cno, c.cname, c.ctel, cf.fno, f.fname
from tbl_camp_facility cf, tbl_camp c, tbl_facility f
where cf.cno=c.cno and cf.fno=f.fno);
select * from view_camp_facility where cno=100;
--캠핑장번호, 캠핑장명, 캠핑장전화, 종류번호,종류이름
create view view_camp_style as
(select cs.cno, c.cname, c.ctel, cs.sno, s.sname
from tbl_camp_style cs, tbl_camp c, tbl_style s
where cs.cno = c.cno and cs.sno = s.sno);
select * from view_camp_style where cno=100;
commit;
Main
import java.sql.Connection;
import java.util.*;
public class Main {
public static void main(String[] args) {
Connection CON=Database.CON;
Scanner s = new Scanner(System.in);
boolean run =true;
CampDAO cdao=new CampDAO();
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("0.프로그램종료 │\n");
System.out.println("└─────────────────────────────┘");
System.out.print("선택>");
String menu = s.nextLine();
switch(menu) {
case "1":
ArrayList<HashMap<String,Object>> array = cdao.list();
for(HashMap<String,Object> map:array) {
System.out.printf("%d\t%-20s\t%s\n",
map.get("no"),
map.get("name"),
map.get("tel"));
}
break;
case "2":
System.out.print("캠핑장번호>"); String cno = s.nextLine();
System.out.println("★★★★★★★★★★★★★★★★");
HashMap<String,Object> map = cdao.read(cno);
if(map.get("name")==null) {
System.out.println("해당 캠핑장 정보가 없습니다.");
}else {
System.out.println("캠핑장이름 :"+map.get("name"));
System.out.println("캠핑장전화 :"+map.get("tel"));
//캠핑장 시설물
ArrayList<HashMap<String,Object>> farray = cdao.facility(cno);
if(farray.size()==0) {
System.out.println("시설물이 없습니다.");
}else {
System.out.print("캠핑장시설물:");
for(HashMap<String,Object> fmap:farray) {
System.out.print(fmap.get("fname")+"|");
}
}
System.out.println("");
//스타일목록
ArrayList<HashMap<String,Object>> sarray = cdao.style(cno);
if(sarray.size()==0) {
System.out.println("스타일이 없습니다.");
}else {
System.out.print("스타일:");
for(HashMap<String,Object> smap:sarray) {
System.out.print(smap.get("sname")+"|");
}
}
}
break;
case "3":
//캠핑장정보
System.out.print("캠핑장번호>"); cno= s.nextLine();
map = cdao.read(cno);
if(map.get("name")==null) {
System.out.println("해당 캠핑장 정보가 없습니다.");
}else {
System.out.println("캠핑장이름 :"+map.get("name"));
System.out.println("캠핑장전화 :"+map.get("tel"));
}
//모든시설물 출력
ArrayList<HashMap<String,Object>> aarray = cdao.all_falcility();
for(HashMap<String,Object> amap:aarray) {
System.out.printf("%d:%s|",amap.get("fno"), amap.get("fname"));
}
// 시설물 등록
System.out.println("");
System.out.print("시설물 등록>"); String fno = s.nextLine();
cdao.tbl_camp_facility(cno, fno);
break;
case "4":
//스타일정보
System.out.print("캠핑장번호>"); cno= s.nextLine();
map = cdao.read(cno);
if(map.get("name")==null) {
System.out.println("해당 캠핑장 정보가 없습니다.");
}else {
System.out.println("캠핑장이름 :"+map.get("name"));
System.out.println("캠핑장전화 :"+map.get("tel"));
}
//모든스타일 출력
ArrayList<HashMap<String,Object>> sarray = cdao.all_style();
for(HashMap<String,Object> smap:sarray) {
System.out.printf("%d:%s|",smap.get("sno"), smap.get("sname"));
}
//스타일 등록
System.out.println("");
System.out.print("스타일 등록>");
String sno = s.nextLine();
cdao.camp_style(cno, sno);
break;
case "0":
run =false;
break;
default:
System.out.println("0~4번 메뉴를 선택하세요.");
}
}
System.out.println("시스템종료!");
}
}
CampDAO
import java.sql.*;
import java.util.*;
public class CampDAO {
//캠핑장목록출력
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("no", rs.getInt("cno"));
map.put("name", rs.getString("cname"));
map.put("tel", rs.getString("ctel"));
array.add(map);
}
}catch(Exception e) {
System.out.println("camp list:"+e.toString());
}
return array;
}
//캠핑장조회
public HashMap<String, Object> read(String no){
HashMap<String, Object> map = new HashMap<>();
try {
String sql = "select * from tbl_camp where cno=?";
PreparedStatement ps = Database.CON.prepareStatement(sql);
ps.setString(1, no);
ResultSet rs = ps.executeQuery();
if(rs.next()) {
map.put("no", rs.getString("cno"));
map.put("name", rs.getString("cname"));
map.put("tel", rs.getString("ctel"));
}
}catch(Exception e) {
System.out.println("camp read:"+e.toString());
}
return map;
}
//캠핑장시설물조회
public ArrayList<HashMap<String,Object>> facility(String no) {
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, no);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
HashMap<String, Object> map = new HashMap<>();
map.put("fno", rs.getInt("fno"));
map.put("fname", rs.getString("fname"));
array.add(map);
}
}catch(Exception e) {
System.out.println("camp facility:"+e.toString());
}
return array;
}
public ArrayList<HashMap<String,Object>> style(String no) {
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, no);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
HashMap<String, Object> map = new HashMap<>();
map.put("sno", rs.getInt("sno"));
map.put("sname", rs.getString("sname"));
array.add(map);
}
}catch(Exception e) {
System.out.println("camp style:"+e.toString());
}
return array;
}
//모든 캠핑장종류
public ArrayList<HashMap<String,Object>> all_style() {
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.getInt("sno"));
map.put("sname", rs.getString("sname"));
array.add(map);
}
}catch(Exception e) {
System.out.println("camp style:"+e.toString());
}
return array;
}
//캠핑장 스타일 등록
public void tbl_camp_facility(String cno, String fno) {
try {
String sql= "insert into tbl_camp_facility(cno,fno) values(?,?)";
PreparedStatement ps = Database.CON.prepareStatement(sql);
ps.setString(1, cno);
ps.setString(2, fno);
ps.execute();
System.out.println("시설물등록완료!");
}catch(Exception e) {
System.out.println("이미등록된 시설물입니다!");
}
}
//시설물 리스트
public ArrayList<HashMap<String,Object>> all_falcility() {
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.getInt("fno"));
map.put("fname", rs.getString("fname"));
array.add(map);
}
}catch(Exception e) {
System.out.println("all_falcility:"+e.toString());
}
return array;
}
//스타일
public void camp_style(String cno, String sno) {
try {
String sql= "insert into tbl_camp_style(cno,sno) values(?,?)";
PreparedStatement ps = Database.CON.prepareStatement(sql);
ps.setString(1, cno);
ps.setString(2, sno);
ps.execute();
System.out.println("시설물등록완료!");
}catch(Exception e) {
System.out.println("이미등록된 시설물입니다!");
}
}
}
새로 배운 개념 : HashMap
HashMap이란 Map인터페이스의 한종류로써 Key와 Value 값으로 데이터를 저장하는 형태를 가지고 있다.
자세한것에 대해서는 새롭게 게시글을 만들예정이다.
댓글남기기