- ✔ DeptVO.java
package edu.global.ex.vo; //이름 널? 유형 //------ -------- ------------ //DEPTNO NOT NULL NUMBER(2) //DNAME VARCHAR2(14) //LOC VARCHAR2(13) public class DeptVO { // 데이터베이스 내의 데이터를 객체화 시켜 메모리에 올리는 것 private int deptno; private String dname; private String loc; public DeptVO() { } // default 생성자 public DeptVO(int deptno, String dname, String loc) { super(); this.deptno = deptno; this.dname = dname; this.loc = loc; } 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 String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc; } }
- ✔ DeptDAO.java
package edu.global.ex.dao; import java.sql.*; import java.util.ArrayList; import java.util.List; import edu.global.ex.vo.DeptVO; public class DeptDAO { private String url = "jdbc:oracle:thin:@localhost:1521:xe"; private String uid = "scott"; private String upw = "tiger"; public DeptDAO() { try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (Exception e) { e.printStackTrace(); } } public List<DeptVO> deptSelect() { List<DeptVO> vos = new ArrayList<DeptVO>(); Connection con = null; Statement stmt = null; ResultSet rs = null; try { String sql = "Select * from dept"; con = DriverManager.getConnection(url, uid, upw); stmt = con.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { // 한번 돌 때마다 테이블의 각 행을 출력 int deptno = rs.getInt("deptno"); String dname = rs.getString("dname"); String loc = rs.getString("loc"); DeptVO vo = new DeptVO(deptno, dname, loc); vos.add(vo); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (con != null) con.close(); } catch (Exception e) { e.printStackTrace(); } } return vos; } }
- ✔ dept_dao.jsp
<%@page import="edu.global.ex.vo.DeptVO"%> <%@page import="java.util.List"%> <%@page import="edu.global.ex.dao.DeptDAO"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% DeptDAO dao = new DeptDAO(); List<DeptVO> vos = dao.deptSelect(); for(DeptVO vo:vos){ out.print("부서번호 : " + vo.getDeptno()+"</br>"); out.print("부서이름 : " + vo.getDname()+"</br>"); out.print("장소 : " + vo.getLoc()+"</br>"); out.print("<hr>"); } %> </body> </html>
- 결과
- ✔ EmpVO.java
package edu.global.ex.vo; import java.util.Date; //이름 널? 유형 //-------- -------- ------------ //EMPNO NOT NULL NUMBER(4) //ENAME VARCHAR2(10) //JOB VARCHAR2(9) //MGR NUMBER(4) //HIREDATE DATE //SAL NUMBER(7,2) //COMM NUMBER(7,2) //DEPTNO NUMBER(2) public class EmpVO { // 데이터베이스 내의 데이터를 객체화 시켜 메모리에 올리는 것 private int empno; private String ename; private String job; private int mgr; private Date hiredate; // 자바 Date 클래스에서 제공되는 함수를 활용할 수 있기때문에 Date형 활용 private int sal; private int comm; private int deptno; public EmpVO() { } // default 생성자 public EmpVO(int empno, String ename, String job, int mgr, Date hiredate, int sal, int 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; } public int getEmpno() { return empno; } public void setEmpno(int empno) { this.empno = empno; } public int getMgr() { return mgr; } public void setMgr(int mgr) { this.mgr = mgr; } public int getSal() { return sal; } public void setSal(int sal) { this.sal = sal; } public int getComm() { return comm; } public void setComm(int comm) { this.comm = comm; } public int getDeptno() { return deptno; } public void setDeptno(int deptno) { this.deptno = deptno; } 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 Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } }
- ✔ EmpDAO.java
package edu.global.ex.dao; import java.sql.*; import java.util.ArrayList; import java.util.List; import edu.global.ex.vo.DeptVO; import edu.global.ex.vo.EmpVO; public class EmpDAO { private String url = "jdbc:oracle:thin:@localhost:1521:xe"; private String uid = "scott"; private String upw = "tiger"; public EmpDAO() { try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (Exception e) { e.printStackTrace(); } } public List<EmpVO> empSelect() { List<EmpVO> vos = new ArrayList<EmpVO>(); Connection con = null; Statement stmt = null; ResultSet rs = null; try { String sql = "Select * from emp"; con = DriverManager.getConnection(url, uid, upw); stmt = con.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { // 한번 돌 때마다 테이블의 각 행을 출력 int empno = rs.getInt("empno"); String ename = rs.getString("ename"); String job = rs.getString("job"); int mgr = rs.getInt("mgr"); Date hiredate = rs.getDate("hiredate"); int sal = rs.getInt("sal"); int comm = rs.getInt("comm"); int deptno = rs.getInt("deptno"); EmpVO vo = new EmpVO(empno,ename,job,mgr,hiredate,sal,comm,deptno); vos.add(vo); // EmpVO vo = new EmpVO(); // vo.setHiredate(hiredate); // setter 함수 활용하여 객체 가져오기 // vos.add(vo); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (con != null) con.close(); } catch (Exception e) { e.printStackTrace(); } } return vos; } }
- ✔ emp_dao.jsp
<%@page import="edu.global.ex.dao.EmpDAO"%> <%@page import="edu.global.ex.vo.EmpVO"%> <%@page import="java.util.List"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% EmpDAO dao = new EmpDAO(); List<EmpVO> vos = dao.empSelect(); String table = "<table = border 1px>"; table += "<tr>"; table += "<th>" + "Empno" + "</th>"; table += "<th>" + "Ename" + "</th>"; table += "<th>" + "Job" + "</th>"; table += "<th>" + "MGR" + "</th>"; table += "<th>" + "Hiredate" + "</th>"; table += "<th>" + "Sal" + "</th>"; table += "<th>" + "Comm" + "</th>"; table += "<th>" + "Deptno" + "</th>"; for (EmpVO vo : vos) { table += "<tr>"; table += "<td>" + vo.getEmpno() + "</td>"; table += "<td>" + vo.getEname() + "</td>"; table += "<td>" + vo.getJob() + "</td>"; table += "<td>" + vo.getMgr() + "</td>"; table += "<td>" + vo.getHiredate() + "</td>"; table += "<td>" + vo.getSal() + "</td>"; table += "<td>" + vo.getComm() + "</td>"; table += "<td>" + vo.getDeptno() + "</td>"; table += "</tr>"; } table += "</table>"; out.print(table); %> </body> </html>
- 결과
* 참고: https://velog.io/@mooh2jj/%EC%BB%A4%EB%84%A5%EC%85%98-%ED%92%80Connection-pool%EC%9D%80-%EC%99%9C-%EC%93%B0%EB%8A%94%EA%B0%80
✔ context.xml 설정하기
<Resource auth="Container" driverClassName="oracle.jdbc.OracleDriver" maxIdle="10" maxTotal="20" maxWaitMillis="-1" name="jdbc/oracle" password="tiger" type="javax.sql.DataSource" url="jdbc:oracle:thin:@127.0.0.1:1521:xe" username="scott" /> <!-- auth : 컨테이너를 자원 관리자로 기술 name : JDBC이름, 변경 가능 driverClassName : JDBC 드라이버 type : 웹에서 이 리소스를 사용할 때 DataSource로 리턴됨 username : 접속계정 password : 접속할 계정 비밀번호 loginTimeout : 연결 끊어지는 시간 maxActive : 최대 연결 가능한 Connection수 (기본 20개) maxIdle : Connection pool 유지를 위해 최대 대기 connection 숫자 maxWait : 사용 가능한 커넥션이 없을 때 커넥션 회수를 기다리는 시간 (1000 = 1초) testOnBorrow : db에 test를 해볼 것인지 -->
✔ DeptDAO.java에서 커넥션풀 활용하기
package edu.global.ex.dao; import java.sql.*; import java.util.ArrayList; import java.util.List; import javax.naming.Context; import javax.naming.InitialContext; import javax.sql.DataSource; import edu.global.ex.vo.DeptVO; public class DeptDAO { // private String url = "jdbc:oracle:thin:@localhost:1521:xe"; // private String uid = "scott"; // private String upw = "tiger"; private DataSource dataSource; // 커넥션 풀 객체 public DeptDAO() { // try { // Class.forName("oracle.jdbc.driver.OracleDriver"); // 커넥션 풀 : context.xml 설정을 통해 미리 메모리에 올려놓음 // } catch (Exception e) { // e.printStackTrace(); // } // } try { Context context = new InitialContext(); // Context => context.xml에서 가져옴 dataSource = (DataSource) context.lookup("java:comp/env/jdbc/oracle"); // context.xml의 name과 이름 맞춰주기 } catch (Exception e) { e.printStackTrace(); } } public List<DeptVO> deptSelect() { List<DeptVO> vos = new ArrayList<DeptVO>(); Connection con = null; Statement stmt = null; ResultSet rs = null; try { String sql = "Select * from dept"; // con = DriverManager.getConnection(url, uid, upw); con = dataSource.getConnection(); // dataSource객체를 활용하여 연결 stmt = con.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { // 한번 돌 때마다 테이블의 각 행을 출력 int deptno = rs.getInt("deptno"); String dname = rs.getString("dname"); String loc = rs.getString("loc"); DeptVO vo = new DeptVO(deptno, dname, loc); vos.add(vo); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (stmt != null) stmt.close(); if (con != null) con.close(); } catch (Exception e) { e.printStackTrace(); } } return vos; } }
스크립트릿 -> ${ }
- ✔ 외래키
CREATE TABLE EMP06 ( EMPNO NUMBER(4) CONSTRAINT EMP06_EMPNO_PK PRIMARY KEY, ENAME VARCHAR2(10) CONSTRAINT EMP06_ENAME_NN NOT NULL, JOB VARCHAR(9), DEPTNO NUMBER(2) CONSTRAINT EMP06_DEPTNO_FK REFERENCES DEPT ( DEPTNO ) ); INSERT INTO EMP06 VALUES(7499,'ALLEN','SALESMAN',70); <-- DEPTNO은 외래키라서 70은 없기때문에 오류발생 -->
CREATE SEQUENCE SAPLE_SEQ; -- 시퀀스 생성 SELECT SAPLE_SEQ.NEXTVAL FROM DUAL; -- 시퀀스 새로운 값 생성 SELECT SAPLE_SEQ.CURRVAL FROM DUAL; -- 시퀀스 값 조회 DROP TABLE EMP01; CREATE TABLE EMP01(EMPNO NUMBER(4) PRIMARY KEY, ENAME VARCHAR(10), HIREDATE DATE); SELECT * FROM EMP01; INSERT INTO EMP01 VALUES (SAPLE_SEQ.NEXTVAL,'JULIA',SYSDATE);
- 결과