1. 드라이버 로딩 (Class.forName(”Oracle.jdbc.OracleDriver”);
2. 커넥션 객체 생성
1. Connection con = DriverManager.getConnection(url, user, password);
2. 오라클 url jdbc:oracle:thin:@localhost:1521:xe
3. Statement 객체 생성
1. String sql= … where first_name=?”; ?=파라미터 매핑할곳
2. preparedStatement stmt = con.prepareStatement(sql);
3. stmt.setXXX(?의 위치, ?에 매핑할 값);
4. stmt.setString(1,emp.getFristName());
4. ResultSet객체 반환
1. ResultSet rs=stnt.executeQuery(); select한 데이터를 반환
2. int rowCount = stmt.executeUpdate(); INSERT, UPDATE, DELETE 구문
5. 결과 소비
1. rs 소비
1. if(rs.next()){
int empid=rs.getInt(”employee_id”);
}else{
throw new RuntimeException(”조회한 데이터가 없음”);
}
EmpVo emp=new EmpVo();
emp.setEmployeeId(rs.get(”employee_id”);
…
empList.add(emp);
}
if(empList.sixe()==0){ //조회한 데이터가 없을경우 예외발생하여 알림
throw new RuntimeException(”조회한 데이터가 없음”)’
}
제약조건 때문이라면 예외가 발생함
where절 조건식이 false인 경우이면 수정 또는 삭제되는 행이 없음.
→ if (rowCount==0){ throw new RuntimeException(”변경된 행이 없음”) };
사원정보를 삭제하는 경우
→ employees테이블과 job_history 테이블의 행을 삭제
→ jobc_history테이블의 데이터 삭제 후 employees테이블의 데이터를 삭제함
삭제가 안될경우 -> 제약조건으로 인한 예외 발생
where 조건이 false여서 삭제되는 행이 없는 경우
con=DriverManager.getConnection();
con.setAtutoCommit(false);
try블록의 맨 아래줄에 con.commit();
catch 블록에 con.rollback();
finally 블록에 con.setAutoCommit(true);