ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA ์ž๋ฐ” JDBC ์—ฐ์Šต - ์ƒํ’ˆ์ •๋ณด ์กฐํšŒํ•˜๊ธฐ
    JAVA 2020. 8. 20. 09:08

    ๐ŸŽฏ ์ž๋ฐ” JDBC ์—ฐ์Šต๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณธ๋‹ค.

     

     

     

     

    JDBC ์ƒํ’ˆ์ •๋ณด ์กฐํšŒํ•˜๊ธฐ

    DB ๊ตฌ์„ฑ

     

    ๊ฒฐ๊ณผ๋Š” ์œ„์™€๊ฐ™์ด ์ถœ๋ ฅ๋˜์–ด์•ผ ํ•œ๋‹ค.

     

    JDBC๋ฅผ ์ด์šฉํ•˜์—ฌ ๋งค์ถœ์ผ์ž์ˆœ, ์ƒํ’ˆ๋ณ„ ๋งค์ถœ์ˆœ ๋ฐ์ดํ„ฐ ์กฐํšŒ๋ฅผ ํ•˜๋„๋ก ๊ตฌํ˜„ํ•œ๋‹ค.

     

     

    1. SQL Developer์—์„œ test ๊ณ„์ • ๋งŒ๋“ค๊ธฐ

    create user test IDENTIFIED by test;
    alter user test account unlock;
    grant connect, resource to test;

    sys์—์„œ ๋ช…๋ น๋ฌธ์„ ์‹คํ–‰ํ•˜๊ณ  ์ƒˆ ๊ณ„์ •์„ ์ƒ์„ฑํ•˜๊ณ  ์ ‘์†ํ•œ๋‹ค.

     

    2. ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ

    DROP TABLE D7_ORDER;
    DROP TABLE D7_PRODUCT;
    
    CREATE TABLE D7_PRODUCT(
    	PID VARCHAR(20) PRIMARY KEY,
    	PNAME VARCHAR(50) NOT NULL,
    	PRICE NUMBER NOT NULL
    );
    
    CREATE TABLE D7_ORDER(
    	OID VARCHAR(10) PRIMARY KEY,
    	PID VARCHAR(20) NOT NULL,
    	QUANTITY NUMBER NOT NULL,
    	ODATE DATE NOT NULL,
    	CONSTRAINT FK_ORDER_PRODUCT FOREIGN KEY (PID) REFERENCES D7_PRODUCT (PID)
    );
    
    INSERT INTO D7_PRODUCT VALUES('P001', '์ด์ž๋…น์Šค', 23000);
    INSERT INTO D7_PRODUCT VALUES('P002', '๋ฆฌ์—”', 8800);
    INSERT INTO D7_PRODUCT VALUES('P003', '์ฃฝ์—ผ์น˜์•ฝ', 5500);
    INSERT INTO D7_PRODUCT VALUES('P004', 'ํŽ˜๋ฆฌ์˜ค', 3400);
    INSERT INTO D7_PRODUCT VALUES('P005', '์˜จ๋”๋ฐ”๋””', 9900);
    INSERT INTO D7_PRODUCT VALUES('P006', '์ƒคํ”„๋ž€', 11500);
    INSERT INTO D7_PRODUCT VALUES('P007', 'ํ…Œํฌ', 9500);
    INSERT INTO D7_PRODUCT VALUES('P008', '๋“œ๋ด‰', 2000);
    INSERT INTO D7_PRODUCT VALUES('P009', '์—˜๋ผ์Šคํ‹ด', 15000);
    INSERT INTO D7_PRODUCT VALUES('P010', '์กฐ์ง€์•„', 1000);
    INSERT INTO D7_PRODUCT VALUES('P011', '์กฐ์ง€์•„-๋ธ”๋ฃจ', 1500);
    INSERT INTO D7_PRODUCT VALUES('P012', 'ํ‚จ์‚ฌ์ด๋‹ค', 1000);
    INSERT INTO D7_PRODUCT VALUES('P013', '์ฝ”์นด์ฝœ๋ผ', 1500);
    INSERT INTO D7_PRODUCT VALUES('P014', '์Šคํ”„๋ผ์ดํŠธ', 2000);
    INSERT INTO D7_PRODUCT VALUES('P015', 'ํ™˜ํƒ€', 1000);
    INSERT INTO D7_PRODUCT VALUES('P016', '์ˆจ', 25000);
    INSERT INTO D7_PRODUCT VALUES('P017', '์ˆ˜๋ คํ•œ', 55000);
    
    INSERT INTO D7_ORDER VALUES('O-001', 'P001', 3, '2014/01/04');
    INSERT INTO D7_ORDER VALUES('O-002', 'P004', 1, '2014/01/04');
    INSERT INTO D7_ORDER VALUES('O-003', 'P001', 10, '2014/01/04');
    INSERT INTO D7_ORDER VALUES('O-004', 'P001', 30, '2014/01/05');
    INSERT INTO D7_ORDER VALUES('O-005', 'P011', 2, '2014/01/05');
    INSERT INTO D7_ORDER VALUES('O-006', 'P012', 1, '2014/01/05');
    INSERT INTO D7_ORDER VALUES('O-007', 'P011', 3, '2014/01/06');
    INSERT INTO D7_ORDER VALUES('O-008', 'P012', 13, '2014/01/07');
    INSERT INTO D7_ORDER VALUES('O-009', 'P013', 3, '2014/01/07');
    INSERT INTO D7_ORDER VALUES('O-010', 'P014', 2, '2014/01/07');
    INSERT INTO D7_ORDER VALUES('O-011', 'P014', 2, '2014/01/08');
    INSERT INTO D7_ORDER VALUES('O-012', 'P001', 1, '2014/01/08');
    INSERT INTO D7_ORDER VALUES('O-013', 'P007', 4, '2014/01/08');
    INSERT INTO D7_ORDER VALUES('O-014', 'P008', 5, '2014/01/08');
    INSERT INTO D7_ORDER VALUES('O-015', 'P001', 10, '2014/01/20');
    INSERT INTO D7_ORDER VALUES('O-016', 'P007', 20, '2014/01/20');
    INSERT INTO D7_ORDER VALUES('O-017', 'P014', 5, '2014/01/20');
    INSERT INTO D7_ORDER VALUES('O-018', 'P013', 3, '2014/01/21');
    INSERT INTO D7_ORDER VALUES('O-019', 'P007', 2, '2014/02/02');
    INSERT INTO D7_ORDER VALUES('O-020', 'P008', 1, '2014/02/02');
    INSERT INTO D7_ORDER VALUES('O-021', 'P008', 1, '2014/02/04');
    INSERT INTO D7_ORDER VALUES('O-022', 'P009', 1, '2014/02/04');
    INSERT INTO D7_ORDER VALUES('O-023', 'P001', 5, '2014/02/04');
    INSERT INTO D7_ORDER VALUES('O-024', 'P012', 5, '2014/03/04');
    INSERT INTO D7_ORDER VALUES('O-025', 'P001', 2, '2014/03/04');
    
    COMMIT;
    
    SELECT TO_CHAR(ODATE, 'YYYY-MM-DD') ์˜์—…์ผ, SUM(O.QUANTITY*P.PRICE) ๋งค์ถœ FROM D7_ORDER O, D7_PRODUCT P
    GROUP BY ODATE
    ORDER BY 1
    ;
    
    SELECT PNAME ์ƒํ’ˆ๋ช…, SUM(O.QUANTITY*P.PRICE) ๋งค์ถœ FROM D7_ORDER O, D7_PRODUCT P
    GROUP BY PNAME
    ORDER BY 2 DESC
    ;

    test ๊ณ„์ •์— ์œ„ ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ๊ณ  ์‹คํ–‰ํ•œ๋‹ค. ์—ฌ๋Ÿฌ๊ฐœ์˜ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋œ๋‹ค.

     

    3. ์ดํด๋ฆฝ์Šค์—์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    String userid = "test";
    String passwd = "test";
    		
    Connection con= null;
    ResultSet rs= null;
    PreparedStatement pstmt= null;

    ๊ธฐ๋ณธ์„ธํŒ…

    try {
    	Class.forName(driver);
    	con = DriverManager.getConnection(url, userid, passwd);
    	String sql = "";

    try๋ฌธ ์‹œ์ž‘ํ•˜๊ณ  ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉํ•œ ๋‹ค์Œ sql ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค.

    Scanner scan = new Scanner(System.in);
    System.out.print("๋งค์ถœ ์กฐํšŒ ๋ฉ”๋‰ด - [ ๋งค์ถœ ์ผ์ž์ˆœ : 1, ์ƒํ’ˆ๋ณ„ ๋งค์ถœ์ˆœ : 2 ]: ");
    int menu = scan.nextInt(); 

    ๋‘๊ฐ€์ง€์˜ ๋งค์ถœ ์กฐํšŒ ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋„๋ก ์Šค์บ๋„ˆ๋ฅผ ์ƒ์„ฑํ•ด ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค.

    if(menu ==1) {
    	sql = "SELECT TO_CHAR(ODATE, 'YYYY-MM-DD') ์˜์—…์ผ, SUM(O.QUANTITY*P.PRICE) ๋งค์ถœ "
    		+ "FROM D7_ORDER O JOIN D7_PRODUCT P USING(PID) GROUP BY ODATE ORDER BY 1";
    }

    ์ˆซ์ž๊ฐ€ 1๋ฒˆ์ผ ๊ฒฝ์šฐ ๋งค์ถœ ์ผ์ž์ˆœ์œผ๋กœ ์˜์—…์ผ๊ณผ ๋งค์ถœ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•œ๋‹ค. ANSI ๋„ค์ธ„๋Ÿด ์กฐ์ธ๋ฐฉ๋ฒ•์— USING์ ˆ์„ ์‚ฌ์šฉํ•ด ๊ณตํ†ต์ปฌ๋Ÿผ์„ ๋ช…์‹œํ•œ๋‹ค.

    ์ˆซ์ž๋ฅผ char๋กœ ํ˜•๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋Š” TO_CHAR(์‚ฌ์šฉํ•  ๋ฐ์ดํ„ฐ, ์›ํ•˜๋Š” ์ถœ๋ ฅ ํ˜•ํƒœ) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ODATE ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋ณ„์นญ์€ ์˜์—…์ผ๋กœ ์ง€์ •ํ•œ๋‹ค. ๋งค์ถœ์ •๋ณด๋Š” ์ฃผ๋ฌธ์ˆ˜๋Ÿ‰๊ณผ ์ƒํ’ˆ๊ฐ€๊ฒฉ์„ ๊ณฑํ•ด SUMํ•จ์ˆ˜๋กœ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•œ๋‹ค.

    else if(menu ==2) {
    	sql = "SELECT PNAME ์ƒํ’ˆ๋ช…, SUM(O.QUANTITY*P.PRICE) ๋งค์ถœ "
    		+ "FROM D7_ORDER O JOIN D7_PRODUCT P USING(PID) GROUP BY PNAME ORDER BY 2 DESC";
    }

    ์ˆซ์ž๊ฐ€ 2๋ฒˆ์ผ ๊ฒฝ์šฐ ์ƒํ’ˆ๋ณ„ ๋งค์ถœ์ˆœ์œผ๋กœ ์ƒํ’ˆ๋ช…๊ณผ ๋งค์ถœ์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋„๋ก ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•œ๋‹ค.

    pstmt = con.prepareStatement(sql);
    	rs = pstmt.executeQuery();
    	System.out.println("--------------------------------------");
    	System.out.println(((menu==1)?"์˜์—…์ผ":"์ƒํ’ˆ๋ช…")+"\t\t\t๋งค์ถœ");
    	System.out.println("--------------------------------------");
    
    while (rs.next()) {
    	System.out.println(rs.getString(1) + "\t\t" + rs.getString(2));
    }
    (menu==1)?"์˜์—…์ผ":"์ƒํ’ˆ๋ช…"

    ์กฐ๊ฑด๋ฌธ ๋Œ€์‹  ๊ฐ„๋‹จํ•œ ์‚ผํ•ญ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. ๊ด„ํ˜ธ ์•ˆ ์กฐ๊ฑด์ด ์ฐธ์ผ ๊ฒฝ์šฐ ?๊ธฐํ˜ธ ๋‹ค์Œ ์ฒซ์ž๋ฆฌ ๊ฐ’์„ ์‹คํ–‰ํ•˜๊ณ  ๊ฑฐ์ง“์ผ ๊ฒฝ์šฐ ๋‘๋ฒˆ์งธ ์ž๋ฆฌ ๊ฐ’์„ ์‹คํ–‰ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ menu๊ฐ’์ด 2์ผ ๊ฒฝ์šฐ์—” ์ƒํ’ˆ๋ช…์„ ์ถœ๋ ฅํ•˜๊ฒŒ ๋œ๋‹ค.

    } catch (ClassNotFoundException e) {
    	e.printStackTrace();
    }catch (SQLException e) {
    	e.printStackTrace();
    } finally {
    	try {
    		if (rs != null)rs.close();
    		if (pstmt != null)pstmt.close();
    		if (con != null)con.close();
    	} catch (SQLException e) {
    		e.printStackTrace();
    	}
    }

    ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•˜๊ธฐ

    #์ „์ฒด ์ฝ”๋“œ ๋ณด๊ธฐ
    
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.Scanner;
    
    public class workshop08 {
    	
    	public static void main(String[] args) throws SQLException {
    		String driver = "oracle.jdbc.driver.OracleDriver";
    		String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    		String userid = "test";
    		String passwd = "test";
    		
    		Connection con= null;
    		ResultSet rs= null;
    		PreparedStatement pstmt= null;
    		
    		try {
    			Class.forName(driver);
    			con = DriverManager.getConnection(url, userid, passwd);
    			String sql = "";
    			Scanner scan = new Scanner(System.in);
    			System.out.print("๋งค์ถœ ์กฐํšŒ ๋ฉ”๋‰ด - [ ๋งค์ถœ ์ผ์ž์ˆœ : 1, ์ƒํ’ˆ๋ณ„ ๋งค์ถœ์ˆœ : 2 ]: ");
    			int menu = scan.nextInt();
    			if(menu ==1) {
    				sql = "SELECT TO_CHAR(ODATE, 'YYYY-MM-DD') ์˜์—…์ผ, SUM(O.QUANTITY*P.PRICE) ๋งค์ถœ "
    						+ "FROM D7_ORDER O JOIN D7_PRODUCT P USING(PID) GROUP BY ODATE ORDER BY 1";
    			}else if(menu ==2) {
    				sql = "SELECT PNAME ์ƒํ’ˆ๋ช…, SUM(O.QUANTITY*P.PRICE) ๋งค์ถœ "
    						+ "FROM D7_ORDER O JOIN D7_PRODUCT P USING(PID) GROUP BY PNAME ORDER BY 2 DESC";
    			}
    			pstmt = con.prepareStatement(sql);
    			rs = pstmt.executeQuery();
    			System.out.println("--------------------------------------");
    			System.out.println(((menu==1)?"์˜์—…์ผ":"์ƒํ’ˆ๋ช…")+"\t\t\t๋งค์ถœ");
    			System.out.println("--------------------------------------");
    			while (rs.next()) {
    				System.out.println(rs.getString(1) + "\t\t" + rs.getString(2));
    			}
    			System.out.println("--------------------------------------");
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}catch (SQLException e) {
    			e.printStackTrace();
    		} finally {
    			try {
    				if (rs != null)
    					rs.close();
    				if (pstmt != null)
    					pstmt.close();
    				if (con != null)
    					con.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}

     

     

     

    EMP_JDBC ํ”„๋กœ์ ํŠธ

    package com.entity;
    
    public class EmpDTO {
    
    	private int empno;
    	private String ename;
    	private String job;
    	private int mgr;
    	private String hiredate;
    	private double sal;
    	private double comm;
    	private int deptno;
    	public EmpDTO() {}
    	public EmpDTO(int empno, String ename, String job, int mgr, String hiredate, double sal, double comm, int deptno) {
    		this.empno = empno;
    		this.ename = ename;
    		this.job = job;
    		this.mgr = mgr;
    		this.hiredate = hiredate;
    		this.sal = sal;
    		this.comm = comm;
    		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 + "]";
    	}
    	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 String getHiredate() {
    		return hiredate;
    	}
    	public void setHiredate(String 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;
    	}
    
    }

    ์ด๋ฒˆ์—” scott๊ณ„์ •์˜ empํ…Œ์ด๋ธ”๋กœ ์—ฐ์Šตํ•ด๋ณธ๋‹ค. empํ…Œ์ด๋ธ”์˜ ๋ชจ๋ธ ํด๋ž˜์Šค๋กœ ์‚ฌ์šฉํ•  DTO ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค.

    package com.exception;
    
    public class DataNotFoundException extends Exception {
    	public DataNotFoundException(String mesg) {
    		super(mesg);
    	}
    
    }

    ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•  ์‚ฌ์šฉ์ž์ •์˜ ์˜ˆ์™ธํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค.

    package com.common.util;
    
    import java.sql.*;
    
    public class JdbcTemplate { // ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ ๋ฐ db์—ฐ๊ฒฐ, con๋ฆฌํ„ด, con.close()
    
    	public static String driver = "oracle.jdbc.driver.OracleDriver";
    	public static String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    	public static String userid = "scott";
    	public static String passwd = "tiger";
    
    	public JdbcTemplate() {
    		// ๋“œ๋ผ์ด๋ฒ„๋กœ๋”ฉ
    		try {
    			Class.forName(driver);
    		} catch (ClassNotFoundException e) {
    			e.printStackTrace();
    		}
    	}
    
    	public static Connection getConnection() {
    		// connect์—ฐ๊ฒฐ ํ›„ tx(ํŠธ๋žœ์žญ์…˜)๋ฅผ false๋กœ ์„ค์ • ๋ฆฌํ„ด con.setAutoCommit(false);
    		Connection conn = null;
    		try {
    			conn = DriverManager.getConnection(url, userid, passwd);
    			conn.setAutoCommit(false); // ์ˆ˜์ •ํ›„ ๋ฆฌํ„ด
    		} catch (Exception e) {
    			System.out.println("[JdbcTemplate.getConnection] : " + e.getMessage());
    			e.getStackTrace();
    		}
    		return conn; // connection ๋ฆฌํ„ด
    	}
    
    	public static void close(Connection conn) {
    		if (isConnected(conn))
    			try {
    				if (isConnected(conn))
    					conn.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    	}
    
    	public static boolean isConnected(Connection conn) {
    		boolean validConnection = true;
    		// conn ์ด null ์ด๊ฑฐ๋‚˜ conn.isClose()๊ฐ€ ์ฐธ์ด๋ฉด validConnection์„ false ๋กœ ์„ค์ •
    		try {
    			if (conn == null || conn.isClosed())
    				validConnection = false;
    		} catch (SQLException e) {
    			validConnection = false;
    			e.printStackTrace();
    		}
    		return validConnection;
    	}
    
    	public static void close(PreparedStatement pstmt) {
    		// dao์—์„œ pstmt๋ฅผ ๋„˜๊ฒจ์ค˜์„œ PreparedStatement๋ฅผ close() //try-catch ์ฒ˜๋ฆฌ
    		try {
    			if (pstmt != null) {
    				pstmt.close();
    			}
    		} catch (SQLException e) {
    			e.getStackTrace();
    		}
    	}
    
    	public static void close(ResultSet rset) {
    		//// dao์—์„œ pstmt๋ฅผ ๋„˜๊ฒจ์ค˜์„œ PreparedStatement๋ฅผ close() close - try - catch ์ฒ˜๋ฆฌ
    		try {
    			if (rset != null)
    				rset.close();
    		} catch (SQLException e) {
    			e.getStackTrace();
    		}
    	}
    
    	public static void commit(Connection conn) {
    		try {
    			if (isConnected(conn)) {
    				conn.commit();
    				System.out.println("[JdbcTemplate.commit] : DB Successfully Committed!");
    			}
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    	}
    
    	public static void rollback(Connection conn) {
    		try {
    			if (isConnected(conn)) {
    				conn.rollback();
    				System.out.println("[JdbcTemplate.rollback] : DB Successfully Rollbacked!");
    			}
    		} catch (SQLException e) {
    			e.printStackTrace();
    		}
    	}
    
    }

    ํ…œํ”Œ๋ฆฟ ํด๋ž˜์Šค๋Š” ํ•„์š”ํ•œ ์ปค๋„ฅ์…˜ ์—ฐ๊ฒฐ๊ณผ ์ž์›๋ฐ˜๋‚ฉ, ์ปค๋ฐ‹ ๋˜๋Š” ๋กค๋ฐฑ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.

    package com.biz;
    
    import java.sql.Connection;
    import java.util.ArrayList;
    
    import com.common.util.JdbcTemplate;
    import com.dao.EmpDAO;
    import com.entity.EmpDTO;
    import com.exception.DataNotFoundException;
    
    public class EmpBiz {
    
    	EmpDAO dao;
    
    	public EmpBiz() {
    		dao = new EmpDAO(); // ํด๋ž˜์Šค ํƒ€์ž… ์ง€์ •x ์ง€์ •ํ•˜๊ฒŒ ๋˜๋ฉด ์ด ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๊ฒŒ๋œ๋‹ค
    	}
    
    	public ArrayList<EmpDTO> selectAllEmp() {
    		// JdbcTemplate.getConnection()์—์„œ con ์–ป๊ธฐ
    		Connection con = JdbcTemplate.getConnection();
    		// dao.selectAllEmp(con); ํ˜ธ์ถœ con ์ „๋‹ฌ
    		ArrayList<EmpDTO> list = dao.selectAllEmp(con);
    		// dbcTemplate ์ด์šฉ close(con)์œผ๋กœ ์ปค๋„ฅ์…˜ ๋Š๊ธฐ
    		JdbcTemplate.close(con);
    		return list;
    	}
    
    	public EmpDTO selectDetailEmp(String eno) throws DataNotFoundException {
    		// JdbcTemplate.getConnection()์—์„œ con ์–ป๊ธฐ
    		Connection con = JdbcTemplate.getConnection();
    		// dao.selectDetailEmp(con); ํ˜ธ์ถœ
    		EmpDTO dto = dao.selectDetailEmp(con, eno);
    		// dbcTemplate ์ด์šฉ close
    		JdbcTemplate.close(con);
    		return dto;
    	}
    
    	public void empUpdate(EmpDTO empDTO) throws DataNotFoundException {
    		Connection con = JdbcTemplate.getConnection();
    		dao.empUpdate(con, empDTO);
    		JdbcTemplate.commit(con);
    		JdbcTemplate.close(con);
    	}
    
    	public void empDelete(String empno) throws DataNotFoundException {
    		Connection con = JdbcTemplate.getConnection();
    		dao.empDelete(con, empno);
    		JdbcTemplate.commit(con);
    		JdbcTemplate.close(con);
    	}
    }

    ๋น„์ฆˆ ํด๋ž˜์Šค๋Š” ํ…œํ”Œ๋ฆฟ ํด๋ž˜์Šค์—์„œ ์ปค๋„ฅ์…˜์„ ์–ป์–ด์˜ค๊ณ  ์ปค๋„ฅ์…˜์„ DAO ํด๋ž˜์Šค์— ๋„˜๊ฒจ์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

    package com.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    
    import com.common.util.JdbcTemplate;
    import com.entity.EmpDTO;
    import com.exception.DataNotFoundException;
    
    public class EmpDAO {
    	public ArrayList<EmpDTO> selectAllEmp(Connection con) {
    		// select ํ›„ jdbcTemplate ์ด์šฉ close
    		ArrayList<EmpDTO> list = new ArrayList<>();
    		PreparedStatement pstmt = null;
    		ResultSet rs = null;
    		try {
    			String sql = "SELECT EMPNO, ENAME, JOB, MGR, TO_CHAR(HIREDATE, 'YYYY-MM-DD') HIREDATE, "
    					+ "SAL, COMM, DEPTNO FROM EMP DORDER BY EMPNO DESC";
    			pstmt = con.prepareStatement(sql);
    			rs = pstmt.executeQuery();
    			while (rs.next()) {
    				int empno = rs.getInt("empno");
    				String ename = rs.getString("ename");
    				String job = rs.getString("job");
    				int mgr = rs.getInt("mgr");
    				String hiredate = rs.getString("hiredate");
    				double sal = rs.getDouble("sal");
    				double comm = rs.getDouble("comm");
    				int deptno = rs.getInt("deptno");
    				EmpDTO notice = new EmpDTO(empno, ename, job, mgr, hiredate, sal, comm, deptno);
    				list.add(notice);
    			}
    		} catch (SQLException e) {
    			e.printStackTrace();
    		} finally {
    			JdbcTemplate.close(rs);
    			JdbcTemplate.close(pstmt);
    		}
    		return list;
    	}
    
    	// ์‚ฌ์›๋ฒˆํ˜ธ๋กœ ์ฐพ๊ธฐ
    	public EmpDTO selectDetailEmp(Connection con, String eno) throws DataNotFoundException {
    		// select ํ›„ jdbcTemplate ์ด์šฉ close
    		EmpDTO result = null;
    		PreparedStatement pstmt = null;
    		ResultSet rs = null;
    		try {
    			String sql = "SELECT EMPNO, ENAME, JOB, MGR, TO_CHAR(HIREDATE, 'YYYY-MM-DD') HIREDATE, "
    					+ "SAL, COMM, DEPTNO FROM EMP WHERE EMPNO=?";
    			pstmt = con.prepareStatement(sql);
    			pstmt.setInt(1, Integer.parseInt(eno));
    			rs = pstmt.executeQuery();
    			if (rs.next()) {
    				int empno = rs.getInt("empno");
    				String ename = rs.getString("ename");
    				String job = rs.getString("job");
    				int mgr = rs.getInt("mgr");
    				String hiredate = rs.getString("hiredate");
    				double sal = rs.getDouble("sal");
    				double comm = rs.getDouble("comm");
    				int deptno = rs.getInt("deptno");
    				result = new EmpDTO(empno, ename, job, mgr, hiredate, sal, comm, deptno);
    			}
    		} catch (SQLException e) {
    			e.printStackTrace();
    		} finally {
    			JdbcTemplate.close(rs);
    			JdbcTemplate.close(pstmt);
    		}
    		return result;
    	}
    
    	public void empUpdate(Connection con, EmpDTO empDTO) {
    		PreparedStatement pstmt = null;
    		try {
    			String sql = "update emp set job = ?, sal=? where empno =?";
    			pstmt = con.prepareStatement(sql);
    			pstmt.setString(1, empDTO.getJob());
    			pstmt.setDouble(2, empDTO.getSal());
    			pstmt.setInt(3, empDTO.getEmpno());
    			int n = pstmt.executeUpdate();
    			System.out.println("์‹คํ–‰๋œ ๋ ˆ์ฝ”๋“œ ๊ฐฏ์ˆ˜:" + n);
    		} catch (SQLException e) {
    			e.printStackTrace();
    		} finally {
    			JdbcTemplate.close(pstmt);
    		}
    	}
    
    	public void empDelete(Connection con, String empno) throws DataNotFoundException {
    		PreparedStatement pstmt = null;
    		try {
    			String sql = "delete from emp where empno = ?";
    			pstmt = con.prepareStatement(sql);
    			pstmt.setInt(1, Integer.parseInt(empno));
    			int n = pstmt.executeUpdate();
    			System.out.println("์‹คํ–‰๋œ ๋ ˆ์ฝ”๋“œ ๊ฐฏ์ˆ˜:" + n);
    		} catch (SQLException e) {
    			e.printStackTrace();
    		} finally {
    			JdbcTemplate.close(pstmt);
    		}
    	}
    }

    DAO ํด๋ž˜์Šค๋Š” sql๋ฌธ์„ ์‹คํ–‰ํ•˜๋Š” ์—ญํ• ์„ ๋‹ด๋‹นํ•œ๋‹ค.

    public class EmpTest {
    	public static void main(String[] args) {
    		EmpBiz empBiz = new EmpBiz();
    
    		ArrayList<EmpDTO> list = empBiz.selectAllEmp();
    		for (EmpDTO dto : list) {
    			System.out.println(dto);
    		}
    
    		try {
    			EmpDTO emp = empBiz.selectDetailEmp("7369");
    			System.out.println(emp);
    		} catch (DataNotFoundException e) {
    			e.printStackTrace();
    		}
    
    		try {
    			empBiz.empDelete("107");
    		} catch (DataNotFoundException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    
    	}// end main
    }// end class

    ๋ฉ”์ธ์—์„œ๋Š” ๋น„์ฆˆ ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

    ์ด ํŒจํ„ด์€ ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค(mybatis)์—์„œ ์‚ฌ์šฉํ•  ํŒจํ„ด๊ณผ ๋น„์Šทํ•˜๋‹ค.

     

     

     

    ๐Ÿ˜œ NEXT : ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ์—ฐ์Šตํ•˜๊ธฐ

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

๊ฐœ๋ฐœ๊ณต๋ถ€