[SIST] JDBC_days02
1. 부서 번호를 입력받아서 emp 테이블 조회하기
hiredate는 Oracle에서 자료형이 DATE인데 일단 String으로 받아와도 문제없이 잘 출력은 된다.
<결과>
[주의!!]
try-catch문 밖에 아래와 같이 코딩이 되어있는데..
Connection conn = null;
Statement stmt = null;
Connection 객체와 Statement 객체는 null로 선언 후 try-catch문 안에서 값을 집어넣어줬기 때문에
try-catch-finally의 finally 문에서 close() 닫는 작업을 해줘야 한다.
현재 ResultSet은 try-catch문 안에서 바로 값을 집어넣었기 때문에 닫는 작업을 하지 않아도 되지만 만약 밖에서 null로 초기 선언을 한다면 동일하게 close(); 작업을 해줘야 한다.
2. DB 연동 클래스 라이브러리 생성
- Driver로딩과 Connection 객체를 생성하는 코딩을 계속 하지 않도록 com.util 패키지 안에 DBconn 클래스 생성을 하였다.
먼저, private static으로 Connection을 null로 선언하고 private 생성자로 지정하여 싱글톤(SingleTon)으로 선언하였다. 즉, 내부 클래스에서만 접근을 할 수 있고 외부 클래스에서는 생성자에 접근할 수 없다.(인스턴스 생성X)
기본적으로 많이 사용하는 SCOTT 계정을 연결하는 Connection 객체를 생성하는 메서드를 생성 후
아래와 같이 url, user, password 값이 바뀌어도 사용할 수 있도록 getConnection 메서드를 오버로딩하여 생성하였다.
그리고 Connection을 닫는 작업을 하는 close() 메서드도 생성하였다.
DBconn (DB 연동 클래스 라이브러리)를 생성한 다음 main 메서드 내에서 테스트를 해보면 잘 실행이 되어지는 것을 확인할 수 있다.
3. ConnectionString.properties 파일에서 url, user, password 가져와서 Connection 객체를 DBconn 클래스를 사용해서 얻어오기
- 위에서 HR 계정으로 연결한 코딩을 아래와 같이 properties파일에서 가져와 연결할 수 있다.
[ConnectionString.properties 파일]
[mian() 메서드 안 코딩]
> Properties에 Reader 객체 필요
> Properties 파일에서 url, user, password 데이터를 읽어오기 위해서 getProperty 사용
4. dept 테이블의 모든 부서정보를 가져와서 ArrayList에 저장 + 부서정보를 출력하는 메서드 생성
1) DTO 란?
- Data Transfer Object
- 데이터를 저장해서 전송하기 위한 객체 ( 어떤 데이터를 담을 수 있는 객체)
- 계층(Layer)간 데이터 저장+전송을 위해 사용하는 객체 => MVC 패턴
> dept 테이블의 컬럼의 자료형들이 다양하기 때문에 DTO 객체를 생성하여 데이터를 담는다.
[DeptDTO 클래스]
[main () 메서드]
[printDept 메서드]
<결과>
5. 부서번호를 입력받아 해당 부서원의 정보를 출력(DTO와 ArrayList 사용)
[EmpDTO 클래스]
위와 같이 private로 field를 선언하고 Alt+Shift+S 단축키로 생성자, getter/setter, toString()를 생성하였다. (아래 코딩)
// constructor
public EmpDTO() {
super();
}
public EmpDTO(int empno, String ename, String job, int mgr, Date hiredate, double sal, double comm, int deptno) {
super();
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}
//getter, setter
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 String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
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 int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "EmpDTO [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate="
+ hiredate + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]";
}
[main() 메서드]
[printEmp 메서드]
<결과>
5. dept 테이블에 DQL, DML문 사용하는 예제
- stmt.executeUpdate(sql)은 int를 반환 -> 레코드(행) 갯수
- stmt.executeQuery(sql)은 ResultSet을 반환
[private static 선언 및 main() 메서드]
[메뉴출력, 메뉴선택, 메뉴처리 메서드]
[조회 : selectAllDept() 메서드]
[추가 : insertDept() 메서드]
> DML문을 수행하는 executeUpdate()는 Auto Commit이 되어서 따로 COMMIT 작업을 해주지 않아도 된다.
[수정 : updateDept() 메서드]
[삭제 : deleteDept() 메서드]
[검색 : searchDept() 메서드]
[종료 : exit() 메서드]
[printDept 메서드]
[일시정지 메서드]
[계속여부확인 메서드]
<결과>