1. 복습문제 2개
1) ConnectionString.properties 파일을 Properties 컬렉션을 사용해서 hostname, sid, user, password 속성값을 읽어 DB 연동 후 HR의 departments 테이블의 부서 정보를 출력하는 코딩을 하세요.
ㄱ. Statement 사용.
ㄴ. ArrayList<DepartmentsDTO> list 사용.
ㄷ. manager_id 가 NULL 인 부서 정보만 출력.
[main() 메서드]
[printDepartment 메서드]
[DepartmentsDTO 클래스]
> 오라클 디벨로퍼에서 DESC departments; 쿼리로 자료형 확인 후 private 변수 선언 및 자동으로 생성자, getter/setter, toString() 생성
package days03;
public class DepartmentsDTO {
private int department_id;
private String department_name;
private int manager_id;
private int location_id;
public DepartmentsDTO() {
super();
}
public DepartmentsDTO(int department_id, String department_name, int manager_id, int location_id) {
super();
this.department_id = department_id;
this.department_name = department_name;
this.manager_id = manager_id;
this.location_id = location_id;
}
public int getDepartment_id() {
return department_id;
}
public void setDepartment_id(int department_id) {
this.department_id = department_id;
}
public String getDepartment_name() {
return department_name;
}
public void setDepartment_name(String department_name) {
this.department_name = department_name;
}
public int getManager_id() {
return manager_id;
}
public void setManager_id(int manager_id) {
this.manager_id = manager_id;
}
public int getLocation_id() {
return location_id;
}
public void setLocation_id(int location_id) {
this.location_id = location_id;
}
@Override
public String toString() {
return "DepartmentsDTO [department_id=" + department_id + ", department_name=" + department_name
+ ", manager_id=" + manager_id + ", location_id=" + location_id + "]";
}
} // class
2) HR의 jobs 테이블에서 job_id 컬럼 또는 job_title 컬럼에
'RE' 또는 're' 또는 'Re' 또는 'rE' ( 즉, 대소문자 구분 없이 ) 문자열을 포함하는 레코드를
검색해서 아래와 같이 출력하세요.
JOB_ID JOB_TITLE
--------------------------------------------------------------
AD_P[RE]S P[re]sident
AD_VP Administration Vice P[re]sident
SA_[RE]P Sales [Re]p[re]sentative
MK_[RE]P Marketing [Re]p[re]sentative
HR_[RE]P Human [Re]sources [Re]p[re]sentative
PR_[RE]P Public [Re]lations [Re]p[re]sentative
[main() 메서드]
[printJobs 메서드]
[JobsDTO 클래스]
> 오라클 디벨로퍼에서 DESC jobs; 쿼리로 자료형 확인 후 private 변수 선언 및 자동으로 생성자, getter/setter, toString() 생성
package days03;
public class JobsDTO {
private String job_id;
private String job_title;
private int min_salary;
private int max_salary;
public JobsDTO() {
super();
}
public JobsDTO(String job_id, String job_title) {
super();
this.job_id = job_id;
this.job_title = job_title;
}
public JobsDTO(String job_id, String job_title, int min_salary, int max_salary) {
super();
this.job_id = job_id;
this.job_title = job_title;
this.min_salary = min_salary;
this.max_salary = max_salary;
}
public String getJob_id() {
return job_id;
}
public void setJob_id(String job_id) {
this.job_id = job_id;
}
public String getJob_title() {
return job_title;
}
public void setJob_title(String job_title) {
this.job_title = job_title;
}
public int getMin_salary() {
return min_salary;
}
public void setMin_salary(int min_salary) {
this.min_salary = min_salary;
}
public int getMax_salary() {
return max_salary;
}
public void setMax_salary(int max_salary) {
this.max_salary = max_salary;
}
@Override
public String toString() {
//return "JobsDTO [job_id=" + job_id + ", job_title=" + job_title + ", min_salary=" + min_salary + ", max_salary=" + max_salary + "]";
return String.format("%s\t%s\t%d\t%d", job_id, job_title, min_salary, max_salary);
}
} // class
2. PreparedStatement
- 하나의 PreparedStatement 객체로 쿼리를 여러 번 처리할 수 있다. (재사용 가능)
ex) Statement는 망치질 할 때 마다 철물점에서 망치를 계속 사오는 것
PreparedStatement는 망치를 하나로 계속 사용하는 것
예시)
[main() 메서드]
[메뉴출력, 메뉴선택, 메뉴처리 메서드]
[ 조회 : selectAllDept() ]
> Statement 객체 대신 PreparedStatement 객체 생성
> 재사용을 하기 때문에 PreparedStatement 객체가 쿼리를 담고 있음
[ 추가 : insertDept() ]
> PreparedStatement는 바인딩 변수(?)를 사용한다.
> ? 에는 홑따옴표를 붙이지 않는다.
> 바인딩 변수(?)를 선언한 만큼 PreparedStatement.setXXX(순서, 값) 메서드로 바인딩 변수에 값을 할당해주어야 한다.
[ 수정 : updateDept() ]
[ 삭제 : deleteDept() ]
[ 검색 : searchDept() ]
[일시정지, printDept, exit, 계속여부확인 메서드]
3. JOIN한 테이블 값 가져오기
> JOIN하여 가져올 컬럼들에 관해 DTO 객체를 생성하면 된다.
[EmpDeptSalDTO 클래스]
자료형 확인 후 아래와 같이 필드 생성 및 생성자, getter/setter, toString() 생성
package days03;
import java.util.Date;
public class EmpDeptSalDTO {
private int deptno;
private String dname;
private int empno;
private String ename;
private Date hiredate;
private String job;
private double sal;
private double comm;
private double pay;
private int grade;
public EmpDeptSalDTO() {
super();
}
public EmpDeptSalDTO(int deptno, String dname, int empno, String ename, Date hiredate, String job, double sal,
double comm, double pay, int grade) {
super();
this.deptno = deptno;
this.dname = dname;
this.empno = empno;
this.ename = ename;
this.hiredate = hiredate;
this.job = job;
this.sal = sal;
this.comm = comm;
this.pay = pay;
this.grade = grade;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
public double getPay() {
return pay;
}
public void setPay(double pay) {
this.pay = pay;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
@Override
public String toString() {
return "EmpDeptSalDTO [deptno=" + deptno + ", dname=" + dname + ", empno=" + empno + ", ename=" + ename
+ ", hiredate=" + hiredate + ", job=" + job + ", sal=" + sal + ", comm=" + comm + ", pay=" + pay
+ ", grade=" + grade + "]";
}
} // class
4. emp, dept, salgrade 테이블 조인한 데이터를 LinkedHashMap에 담기
아래와 같은 실행 결과를 출력하고 각 등급의 데이터를 등급은 KEY, 값은 VALUE로 LinkedHashMap에 담는 예제이다.
[실행결과]
1등급 ( 700~1200 ) - 2명 > SalgradeDTO(KEY 값으로 사용)
20 RESEARCH 7369 SMITH 800 > ArrayList<EmpDeptSalDTO> list (VALUE 값으로 사용)
30 SALES 7900 JAMES 950
2등급 ( 1201~1400 ) - 2명
30 SALES 7654 MARTIN 2650
30 SALES 7521 WARD 1750
3등급 ( 1401~2000 ) - 2명
30 SALES 7499 ALLEN 1900
30 SALES 7844 TURNER 1500
4등급 ( 2001~3000 ) - 4명
10 ACCOUNTING 7782 CLARK 2450
20 RESEARCH 7902 FORD 3000
20 RESEARCH 7566 JONES 2975
30 SALES 7698 BLAKE 2850
5등급 ( 3001~9999 ) - 1명
10 ACCOUNTING 7839 KING 5000
[mian() 메서드]
[printSalgradeEmp 메서드]
'TIL > JDBC' 카테고리의 다른 글
[SIST] JDBC_days05_Java에서 트랜잭션 처리와 CallableStatement (0) | 2022.05.10 |
---|---|
[SIST] JDBC_days05_페이징 처리하기 (0) | 2022.05.10 |
[SIST] JDBC_days04 (0) | 2022.05.09 |
[SIST] JDBC_days02 (0) | 2022.05.03 |
[SIST] JDBC_days01 (0) | 2022.05.02 |