Database

오라클 hr계정 SQL문 예제, Servlet Context 생성 - 2020-05-26

리차드 2020. 5. 26. 19:11

오늘 수업 내용

1. 오라클 hr계정을 이용한 SQL문 예제 6문제

2.   <입력>

     html작성 -> java작성 -> web.xml작성

     <실행>

     html실행 후 입력-> submit 또는 button -> java문 실행하여 결과 보여주기

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

hr계정에서 진행되었음

select * from employees;


--문제1 : 다음처럼 출력하시오

select employee_id as 사원번호, 

        first_name||' '||last_name as "이  름",

        salary*12||'달러' as "연  봉"

                 from employees;

 

 

 

 

 

 

 

 

 

 

 

 

--문제2 : 다음처럼 출력하시오 (last_name, job_id)
select last_name||' is a '||job_id as "Employee Detail" 

                 from employees;

 

 

 

 

--문제3 : 급여가 2500이하 이거나 3000이상이면서 90번 부서인 사원의 이름, 급여, 부서ID를 출력하시오.
select first_name||' '||last_name as 사원명, 

        '$'||salary as 월급, department_id as 부서코드

                from employees

                where (salary >= 30000 or salary <= 25000) and department_id = 90;

 

 

 


--문제4 : 업무ID가 'SA_REP' 이거나 'AD_PRES' 이면서 급여가 10000를 초과하는 사원들의 이름, 업무ID, 급여를 출력하시오
select last_name as 이름, 

        job_id as 업무ID,

        salary||'원' as "급 여"

                from employees

                where job_id in ('AD_PRES', 'SA_REP') and salary > 10000;

 

 

 

 

--문제5 : Employees테이블의 업무ID가 중복되지 않게 표시하는 질의를 작성하시오
select distinct job_id as 업무ID 

              from employees;

 

 


--문제6 : 입사일이 2005년인 사원들의 사원번호, 이름, 입사일을 표시하시오
select employee_id as 사원번호, 

        first_name||' '||last_name as 이름,

        to_char(hire_date, 'yyyy/mm/dd') as 입사일

              from employees

              where to_char(hire_date, 'yy') = 05;

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

SQL 예제를 통해 새롭게 알게 된 내용

 

- 데이터에 값을 추가해서 보여줄 때 ||를 사용

- 데이터에 문자를 추가해서 보여줄 때 '' Single Quotation 사용

- as로 별명을 붙여줄 때 별명에 공백이 있나요 ? Quotation 생략불가 : Quotation 생략가능;

- or의 또다른 형식 in (a, b)

     ex) where job_id in('FI_MGR', 'FI_ACCOUNT');

    위 문장은 where (job_id = 'FI_MGR') or (job_id = 'FI_ACCOUNT'); 와 같다

- and의 또다른 형식 between a and b (이상 이하로 적용됨을 체크)

     ex) where salary between 10000 and 20000;

    위 문장은 where (salary >= 10000) and (salary <= 20000); 과 같다

- 레코드 세기 select count(*) from employees(테이블명);

- 데이터 직접 계산 후 보여주기가 가능하다 ex) select salary*12 as 연봉 from employees;

- char값 비교시 대소문자로 인한 혼란 방지 where lower(last_name) = 'king';

- date값 비교시 to_char(date컬럼명, '리턴형식') 을 이용한다

    ex) 2005년 입사자를 구하려면 hire_date 컬렴을 where to_char(hire_date, 'yy') = 05 하여 활용

 

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

 

Servlet

C:\Tomcat\webapps 에서 모든 일들이 이뤄진다.

지금까지는 ROOT 폴더 내에서 작업하다가

오늘은 처음으로 ROOT 이외의 폴더, 즉 새로운 Context를 생성하여 작업하는 것을 해봤다.

 

1. parameter.html을 작성한다 (저장은 C:\Tomcat\webapps\ROOT폴더 안) : https://codepen.io/hj-rich/pen/JjYVXzj

2. ParamServlet.java를 작성, 컴파일한다(ROOT\WEB-INF\src, classes) : https://codepen.io/hj-rich/pen/JjYVmRe

6. C:\Tomcat\webapps\WEB-INF\web.xml 에서 servlet과 mapping을 설정한다.

 

localhost:8080/parameter.html 을 실행 후 값을 입력 후 submit 하면 ParamServlet.java가 동작한다.

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

위는 default Context인 ROOT폴더를 이용한 경우다.

지금은 testServlet 라는 Context를 C:\Tomcat\webapps\testServlet 폴더를 생성해서 진행한다.

 

지금부터 최상의 경로를 C:\Tomcat\webapps\testServlet 로 전제한다.

 

1. webapps 안에 testServlet 이라는 폴더를 만들어서 새로운 Context를 준비한다.

2. 해당 폴더 안에 WEB-INF 라는 폴더와 input.html 파일을 만들고 작성한다. https://codepen.io/hj-rich/pen/zYvXmPm

3. WEB-INF 폴더 안에 web.xml 파일, src 폴더, lib 폴더, classes 폴더를 준비한다.

4. web.xml 파일의 서블릿 설정을 한다.

5. src 폴더 내 ResultServlet.java를 작성하고 컴파일한다 : https://codepen.io/hj-rich/pen/pojBYJO

 

localhost:8080/testServlet/input.html 을 실행 후 값을 입력 후 계산 누르면 ResultServlet.java 가 동작한다.

 

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

오늘 숙제였던 testServlet2 Context 작업이다

 

Context : testServlet2

html : person.html https://codepen.io/hj-rich/pen/gOayZYj

java : PersonServlet.java https://codepen.io/hj-rich/pen/gOayZyV

 

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

Servlet, html 관련하여 새롭게 알게 된 점

- Context는 webapps 폴더 내의 폴더를 생성하여 만든다.

- html파일은 Context폴더 안에 저장한다.

- Context마다 기본적으로 설정할 폴더와 파일은 WEB-INF 폴더 내의 web.xml파일과 src, classes, lib 폴더이다.

- web.xml 파일을 통해 Context를 구분한다. classes 폴더 내의 파일을 알아서 찾아간다.

- 404 Error 500 Error를 많이 만났는데, init()메소드와 doGet()메소드에 sysout을 하나씩 줘서 확인한다.

- 주로 xml파일 설정, 파일의 경로설정 등의 문제였고 500 Error 인 경우 Error Message를 잘 확인해 처리한다

- ROOT Context는 Servlet name을 ParamServlet으로 줬을 경우 http://localhost:8080/ParamServlet 이렇게 되지만

- testServlet Context는 Servlet name 이 ResultServlet이고 http://localhost:8080/testServlet/ResultServlet 일케된다

- html에서 name="" value="" 로 변수명과 값을 정하고 java request 객체의 getParameter("변수명")로 값을 가져옴

- java response의 getWriter()객체 생성후 해당 객체의 print 또는 println 메시지로 html문을 작성함.

- html에서 여러 값을 가져올 경우 getParameterValues()메소드를 이용해 스트링 배열로 받는다

  ex) String[] hobby = request.getParameterValues("hobby");

 

 

별거 아닌 거 같은데 Context 를 새로 생성할 때 html, xml, java, class 파일의 경로와 호출시 경로지정이 어려웠다.

 

후앙.

이거 어찌 다 기억하냐...

기사공부하러.... 흠 ㅠ