Dao dao = new Dao();
Vo vo = new Vo();
String empno = request.getParameter("empno");
vo.setEmpno(empno);
dao.sub2(empno);
List<Vo> list = dao.sub2(empno);
if(list.size() != 0) {
response.sendRedirect("sub2list.jsp?empno="+empno);
}else{
response.sendRedirect("sub2null.jsp");
}
Dao dao = new Dao();
String empno = request.getParameter("empno");
List<Vo> list = dao.sub2(empno);
🔨 문제
📎 근무좌석예약조회
사원번호를 입력하세요 좌석예약조회 다시쓰기
📎 사원번호가 존재할 경우
<사원번호 1001님의 좌석예약조회>
사원번호 이름 근무일자 좌석번호 좌석위치 내선번호 1001 김사원 2021년 10월 05일 S008 1층 103호 7777-0008 1001 김사원 2021년 10월 04일 S002 1층 101호 7777-0002 돌아가기
📎 사원번호가 존재하지 않을 경우
<1000님의 좌석예약정보가 존재하지 않습니다>
돌아가기
🔨 테이블 및 데이터
📎 테이블
-- 사원정보 테이블 명세서 create table tbl_emp ( empno char(4) not null, -- 사원번호 empname varchar2(20), -- 사원성명 tel1 char(3), -- 전화번호 1 tel2 char(4), -- 전화번호 2 tel3 char(4), -- 전화번호 3 deptcode char(2), -- 부서코드 position char(1), --직위 constraint tbl_emp_pk primary key (empno) );-- 좌석정보 테이블 create table tbl_seat ( seatno char(4) not null, -- 좌석번호 office varchar2(20), -- 좌석위치 callno varchar2(10), -- 내선번호 useflag char(1), -- 예약가능유무 / N:불가, Y:가능 constraint tbl_seat_pk primary key (seatno) );-- 근무좌석예약정보 테이블 create table tbl_resv ( resvno char(8) not null, -- 예약번호 empno char(4), -- 사원번호 resvdate char(8), -- 예약일자 seatno char(4), -- 좌석번호 constraint tbl_resv_pk primary key (resvno) );
📎 데이터
insert into tbl_emp values ('1001','김사원','010','1234','1111','10','1'); insert into tbl_emp values ('1002','이사원','010','1234','2222','20','2'); insert into tbl_emp values ('1003','박사원','010','1234','3333','30','3'); insert into tbl_emp values ('1004','홍사원','010','1234','4444','10','4'); insert into tbl_emp values ('1005','조사원','010','1234','5555','20','1'); insert into tbl_emp values ('1006','정사원','010','1234','6666','30','2'); insert into tbl_emp values ('1007','황사원','010','1234','7777','10','3');insert into tbl_seat values ('S001','1층101호','7777-0001','N'); insert into tbl_seat values ('S002','1층101호','7777-0002','Y'); insert into tbl_seat values ('S003','1층101호','7777-0003','Y'); insert into tbl_seat values ('S004','1층102호','7777-0004','Y'); insert into tbl_seat values ('S005','1층102호','7777-0005','Y'); insert into tbl_seat values ('S006','1층102호','7777-0006','N'); insert into tbl_seat values ('S007','1층103호','7777-0007','Y'); insert into tbl_seat values ('S008','1층103호','7777-0008','Y'); insert into tbl_seat values ('S009','1층103호','7777-0009','Y');insert into tbl_resv values ('20210001','1001','20211004','S002'); insert into tbl_resv values ('20210002','1002','20211004','S003'); insert into tbl_resv values ('20210003','1003','20211004','S004'); insert into tbl_resv values ('20210004','1004','20211004','S005'); insert into tbl_resv values ('20210005','1005','20211005','S004'); insert into tbl_resv values ('20210006','1006','20211005','S005'); insert into tbl_resv values ('20210007','1007','20211005','S007'); insert into tbl_resv values ('20210008','1001','20211005','S008'); insert into tbl_resv values ('20210009','1002','20211006','S002'); insert into tbl_resv values ('20210010','1003','20211006','S003');
📎 데이터 화면표시 조건
Stirng dept = null; switch (deptcode) { case "10": dept = "영업팀"; break; case "20": dept = "총무팀"; break; case "30": dept = "구매팀"; break; }
🔨 푸는 방법 생각하기
1. 출력해야할 것
사원번호(empno)가 있을 때
empno, empname, resvdate, seatno, office, callno
사원번호(empno)가 없을 때
empno
2. SQL문 작성하기
select e.empno,
e.empname,
r.resvdate,
s.seatno,
s.office,
s.callno
from tbl_emp e, tbl_resv r, tbl_seat s
where e.empno=r.empno and
s.seatno=r.seatno and
e.empno=?
order by resvdate;
3. 키보드로 empno 값을 받아서 일치하는 데이터를 찾아 List에 담는다
4. 리스트가 존재하는 경우 empno를 좌석예약조회 페이지로 넘겨 데이터를 출력한다
5. 리스트가 존재하지 않는 경우 empno를 좌석예약조회실패 페이지로 넘겨 empno를 출력한다
🔨 정답
Dao
public List<Vo> sub2(String empno) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; String sql = "select e.empno, e.empname, r.resvdate, s.seatno, s.office, s.callno " + " from tbl_emp_202108 e, tbl_resv_202108 r, tbl_seat_202108 s " + " where e.empno=r.empno and s.seatno=r.seatno and e.empno=? " + " order by resvdate "; List<Vo> list = new ArrayList<Vo>(); try { conn = DBmanager.getInstance().getConnection(); pstmt = conn.prepareStatement(sql); pstmt.setString(1, empno); rs = pstmt.executeQuery(); while(rs.next()) { Vo vo = new Vo(); vo.setEmpno(rs.getString("empno")); vo.setEmpname(rs.getString("empname")); vo.setResvdate(rs.getString("resvdate")); vo.setSeatno(rs.getString("seatno")); vo.setOffice(rs.getString("office")); vo.setCallno(rs.getString("callno")); list.add(vo); } }catch (Exception e) { e.printStackTrace(); }finally { try { if(rs != null) { rs.close(); } if(pstmt != null) { pstmt.close(); } if(conn != null) { conn.close(); } }catch(Exception e) { e.printStackTrace(); } } return list; }
sub2.jsp
<section> <h2>좌석예약조회</h2> <form name="my" method="post" action="sub2pro.jsp" onsubmit="return check()"> <table> <tr> <th>사원번호를 입력하시오.</th> <td> <input type="text" name="empno" id="empno"> </td> </tr> <tr> <td colspan="2" style="text-align:center;"> <button type="submit">좌석예약조회</button> <a href="index.jsp"> <button>홈으로</button> </a> </td> </tr> </table> </form> </section>
sub2pro.jsp
request.setCharacterEncoding("utf-8"); Dao dao = new Dao(); Vo vo = new Vo(); String empno = request.getParameter("empno"); vo.setEmpno(empno); dao.getSub2Insert(empno); List<Vo> list = dao.sub2(empno); if(list.size() != 0) { response.sendRedirect("sub2list.jsp?empno="+empno); }else{ response.sendRedirect("sub2null.jsp"); }
sub2list.jsp (사원번호가 존재할 경우)
Dao dao = new Dao(); String empno = request.getParameter("empno"); List<Vo> list = dao.getSub2Insert(empno);<section> <h2>사원번호 : <%= empno %>님의 좌석예약조회</h2> <table> <tr> <th>사원번호</th> <th>이름</th> <th>근무일자</th> <th>좌석번호</th> <th>좌석위치</th> <th>내선번호</th> </tr> <% for(Vo vo : list) { %> <tr> <td><%= vo.getEmpno() %></td> <td><%= vo.getEmpname() %></td> <td> <%= vo.getResvdate().substring(0,4) %>년 <%= vo.getResvdate().substring(4,6) %>월 <%= vo.getResvdate().substring(6) %>일 </td> <td><%= vo.getSeatno() %></td> <td><%= vo.getOffice() %></td> <td><%= vo.getCallno() %></td> </tr> <% } %> <tr> <td colspan="6" style="text-align:center"> <a href="sub2.jsp"> <button>돌아가기</button> </a> </tr> </table> </section>
sub2null.jsp (사원번호가 존재하지 않을 경우)
<section> <h2>좌석 예약 정보가 존재하지 않습니다</h2> <div class="button"> <a href="sub2.jsp"> <button>돌아가기</button> </a> </div> </section>