Oracle requires minimal changes from the MySQL configuration except for the
usual gotchas :-)
Drivers for older Oracle versions may be distributed as *.zip files rather
than *.jar files. Tomcat will only use *.jar files installed in
$CATALINA_HOME/common/lib. Therefore classes111.zip
or classes12.zip will need to be renamed with a .jar
extension. Since jarfiles are zipfiles, there is no need to unzip and jar these
files - a simple rename will suffice.
For Oracle 9i onwards you should use oracle.jdbc.OracleDriver
rather than oracle.jdbc.driver.OracleDriver as Oracle have stated
that oracle.jdbc.driver.OracleDriver is deprecated and support
for this driver class will be discontinued in the next major release.
1. Context configuration
In a similar manner to the mysql config above, you will need to define your
Datasource in your Context. Here we define a
Datasource called myoracle using the thin driver to connect as user scott,
password tiger to the sid called mysid. (Note: with the thin driver this sid is
not the same as the tnsname). The schema used will be the default schema for the
user scott.
Use of the OCI driver should simply involve a changing thin to oci in the URL string.
You can use the same example application as above (asuming you create the required DB
instance, tables etc.) replacing the Datasource code with something like
>>문자값 중 대문자를 소문자로 변환한다.
※ 오라클은 테이블에 저장되어있는 데이터에 대해서 대소문자를 구분합니다.
예를들어 'King'이라는 사원의 이름이 있다면, WHERE절로 질의문을 써서 조건을 줄때 'king'로 했다면, 검색이 안된다.
EX) WHERE last_name = 'seo'
결과 : no roew selected라고 검색된 레코드가 없다고 메세지를 준다.
(2) SUBSTR 함수
SUBSTR(문자열, 시작 문자 위치, 반환할 문자 갯수) >>입력된 문자열을 시작 문자 위치에서 부터 문자 갯수 만큼 반환한다.
SQL>
SELECT SUBSTR('Korea Economy', 1, 5) "subtr"
FROM dual;
>>'Korea Economy' 문자열의 시작위치 1부터 5까지의 문자를 반환한다.
결과 : Korea
2. 오라클 - ORDER BY, GROUP BY, HAVING
1) ORDER BY - 데이터 정렬해서 보기
ORDER BY 칼럼이름 [ASC|DESC]
※ ORDER BY에 기본은 ASC정렬(오름차순)이다.
ASC 오름차순 - 작은 값부터 큰 값
DESC 내림차순 - 큰 값부터 작은 값
● 오름차순으로 정렬하기
SQL>
SELECT employee_id, last_name, salary
FROM employees
WHERE salary >= 10000 ORDER BY salary;
>> employees 안에 있는 데이터 중 salary >= 10000 의 조건을 만족하는 데이터의 employee_id, last_name, salary 칼럼을 조회한다. 이때 salary의 데이터를 기준으로 오름차순으로 출력한다.
● 내림차순으로 정렬하기
SQL>
SELECT employee_id, last_name, salary
FROM employees
WHERE salary >= 10000 ORDER BY salary DESC;
>> employees 안에 있는 데이터 중 salary >= 10000 의 조건을 만족하는 데이터의
employee_id, last_name, salary 칼럼을 조회한다. 이 때 salary의 데이터를 기준으로 내림차순으로
출력한다.
2) GROUP BY - 데이터 그룹화하기
GROUP BY 칼럼이름
※칼럼의 동일한 데이터끼리 그룹으로 묶는다
● 그룹화하여 평균 구하기 SQL>
SELECT department_id, AVG(salary)
FROM employees GROUP BYdepartment_id ORDER by department_id;
>> employees 안에 있는 데이터 중 department_id, AVG(salary)를 조회한다.
조회할 때 부서별(department_id)로 그룹화 한 후 부서별 급여(salary)의 평균을 구한다. 그 후에
department_id를 기준으로 오름차순으로 출력한다.
※ AVG(salary)는 salary의 평균값을 구하는 공식.
● 연도별 그룹화하기 SELECT TO_CHAR(hire_date, 'yyyy'), AVG(salary)
FROM employees
GROUP BY TO_CHAR(hire_date, 'yyyy');
>> employees 안에 있는 데이터 hire_date, salary의 데이터를 hire_date의 연도별로 그룹화 한 후, 연도별 평균 값을 출력한다.
※TO_CHAR(hire_date, 'yyyy') 는 hire_date의 연도(yyyy)의 값을 구한다. TO_CHAR에서 더 설명함.
3) HAVING - GROUP BY의 결과로부터 특정 조건을 만족하는 것만 조회하기
GROUP GY 칼럼이름 HAVING 조건
※그룹으로 묶어진 칼럼에서 조건에 맞는 그룹만 조회한다.
●조건에 만족하는 그룹만 출력하기
SQL>
SELECT department_id, AVG(salary)
FROM employees GROUP BY department_id HAVING AVG(salary0>=10000;
>> employees 안의 데이터를 동일한 department_id 별로 그룹으로 묶어 그룹별 급여평균이 10000이상인 그룹만 조회한다.
● 기본적인 조건 조회하기
SQL) SELECT employee_id, hire_date
FROM employees WHERE last_name = 'king';
>> employees 안에 있는 데이터 중 last_name= 'king'인 데이터의 employee_id, hire_date 칼럼을 조회한다.
SQL) SELECT employee_id, last_name, salary
FROM employees WHERE salary >= 10000;
>> employees 안에 있는 데이터 중 salary >= 10000 의 조건을 만족하는 데이터의 employee_id, last_name, salary 칼럼을 조회한다.
※ 테이블에 저장된 문자 데이터에 대해서는 대소문자를 구분한다. 그리고 문자, 날짜 데이터는 ' '으로 묶어서 표현해 주어야한다.
● AND를 이용한 조건 설정
SQL) SELECT employee_id, last_name, salary
FROM employees
WHERE salary >= 10000
AND salary <= 20000;
>> employees 안에 있는 데이터중 salary >= 10000, salary <=20000 의 두 조건을 모두 만족하는 데이터의 employee_id, last_name, salary을 조회한다.
● OR를 이용한 조건 설정
SQL) SELECT employee_id, last_name, salary
FROM employees
WHERE salary >= 10000
OR salary <= 20000;
>> employees 안에 있는 데이터 중 salary >= 10000, salary <=20000 의 두 조건중 하나라도 만족하는 데이터의 employee_id, last_name, salary을 조회한다.
● BETWEEN AND를 이용한 조건 설정. (AND 연산을 보다 간결하게)
SQL) SELECT employee_id, last_name, salary
FROM employees
WHERE salary BETWEEN 10000 AND 20000;
>> 위의 AND를 이용한 조건설정을 BETWEEN AND를 이용해 보다 간편하게 표현할수 있다.
(BETWEEN AND 연산은 =<, => 과 같은 말이다. >, <와 같은 미만,초과와 같은 표현은 AND연산으로 비교해야함.)
● IN를 이용한 조건 설정 (OR연산을 보다 간결하게)
SQL) SELECT employee_id, last_name, job_id
FROM employees
WHERE job_id = 'FI_MGR'
OR job_id = 'FI_ACCOUNT'
OR job_id = 'SA_MGR';
위의 부분을
WHERE job_id IN ('FI_MGR', 'FI_ACCOUNT','SA_MGR');
으로 보다 간결하게 표현할수 잇다.
>> 위 IN조건 중 하나라도 만족하는 데이터의 employee_id, last_name, job_id를 조회한다.
WHERE commission_pct IS NOT NULL; //IS NULL일 경우에는 값이 있는 것을 조회한다.
>> employees 안에 있는 데이터 중 commission_pct의 값이 NULL이 아닌 데이터의 employee_id, last_name, job_id, salary, commision_pct를 조회한다.
※ NULL은 0이 아니다. 할당되지 않은 값을 말한다. 즉, commission_pct에 아무런 값이 없을때를 말함.
commission_pct의 값에 0이 들어가있다면, 그것은 0이라는 값이 할당된것이다.
● LIKE를 이용한 조건 설정
SQL> SELECT employee_id, last_name, hire_date
FROM employees
WHERE hire_date BEETWEEN '97/01/01' AND '97/12/31'
위의 노란 부분을
WHERE hire_date LIKE '97%';
이렇게 바꿀수 있다.
hire_date는 입사일임.
>>두 구문은 hire_date안의 97년 입사자를 조회한다. 97%의 의미는 '97'로 시작되는 모든 값은
조건을 만족하며, 만약에 사원이름을 검색할때 일부만 기억하고 있을경우 'Au'로 LIKE를 이용하여 조건절을 설정하면 'Au'로
시작하는 사원을 조회한다.
※ 연도를 이용하여 조건을 검색할때는 2자리로 검색하는 것보다, 4자리로 검색하는게 좋다. 1970년과 2070년의 구분의 의미가 없어지기 때문이다.