GiantStepDEV

Common

  • ํ”„๋กœ์ ํŠธ ๋‚ด์—์„œ ๊ณตํ†ต์œผ๋กœ ์‚ฌ์šฉํ•  ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„
package com.kh.jdbc.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Common {
    final static String ORACLE_URL = "jdbc:oracle:thin:@localhost:1521:xe";
    final static String ORACLE_ID = "SCOTT";
    final static String ORACLE_PWD = "TIGER";
    final static String ORACLE_DRV = "oracle.jdbc.driver.OracleDriver";

    public static Connection getConnection() {
        Connection conn = null; // Connection์€ ๋ฐ˜๋“œ์‹œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ•ด์ค˜์•ผ ํ•จ
        try {
            Class.forName(ORACLE_DRV); // ์˜ค๋ผํด ์—ฐ๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ๋“œ๋ผ์ด๋ธŒ ๋กœ๋”ฉ
            conn = DriverManager.getConnection(ORACLE_URL, ORACLE_ID, ORACLE_PWD); // ์ž๋ฐ”-์˜ค๋ผํด ์—ฐ๊ฒฐ ์–ป๊ธฐ
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void close(Connection conn) {
        try {
            if(conn != null && !conn.isClosed()) {
                conn.close();
                System.out.println("Connection ํ•ด์ œ ์„ฑ๊ณต");
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void close(Statement stmt) {
        try {
            if(stmt != null && !stmt.isClosed()) {
                stmt.close();
                System.out.println("Connection ํ•ด์ œ ์„ฑ๊ณต");
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    public static void close(ResultSet rSet) {
        try {
            if(rSet != null && !rSet.isClosed()) {
                rSet.close();
                System.out.println("Connection ํ•ด์ œ ์„ฑ๊ณต");
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

EmpDAO

  • ๊ฐ ์ฟผ๋ฆฌ๋ฌธ ์‹คํ–‰์‹œํ‚ฌ ๋ฉ”์†Œ๋“œ ๊ตฌํ˜„
package com.kh.jdbc.dao;
import com.kh.jdbc.util.Common;
import com.kh.jdbc.vo.EmpVO;
import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class EmpDAO {
    Connection conn = null; // ์ž๋ฐ”์™€ ์˜ค๋ผํด์— ๋Œ€ํ•œ ์—ฐ๊ฒฐ ์„ค์ •
    Statement stmt = null; // sql๋ฌธ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ๊ฐ์ฒด
    ResultSet rs = null; // statement ๋™์ž‘์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋กœ ์ „๋‹ฌ๋˜๋Š” DB์˜ ๋‚ด์šฉ..!
    Scanner sc = new Scanner(System.in);

    public List<EmpVO> empSelect() {
        List<EmpVO> list = new ArrayList<>(); // ๋ฐ˜ํ™˜ํ•  ๋ฆฌ์ŠคํŠธ๋ฅผ ์œ„ํ•ด list ๊ฐ์ฒด ์ƒ์„ฑ
        try {
            conn = Common.getConnection();
            stmt = conn.createStatement();
            String sql = "SELECT * FROM EMP";
            rs = stmt.executeQuery(sql); // SELECT ๋ฌธ๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ(ํ–‰)๋กœ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜ ๋  ๋•Œ ์‚ฌ์šฉ

            while (rs.next()) { // ์ฝ์„ ํ–‰์ด ์žˆ์œผ๋ฉด ์ฐธ
                int no = rs.getInt("EMPNO");
                String name = rs.getString("ENAME");
                String job = rs.getString("JOB");
                int mgr = rs.getInt("MGR");
                Date date = rs.getDate("HIREDATE");
                double sal = rs.getDouble("SAL");
                double comm = rs.getDouble("COMM");
                int deptNo = rs.getInt("DEPTNO");
                EmpVO vo = new EmpVO(no, name, job, mgr, date, sal, comm, deptNo); // ํ•˜๋‚˜์˜ ํ–‰์— ๋Œ€ํ•œ ์ •๋ณด ์ €์žฅ์„ ์œ„ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ
                list.add(vo);
            }
            // ์—ฐ๊ฒฐ๊ณผ ์—ญ์ˆœ์œผ๋กœ ํ•ด์ œ
            Common.close(rs);
            Common.close(stmt);
            Common.close(conn);

        } catch (Exception e) {
            e.printStackTrace(); // ๋„˜์–ด์˜ค๋Š”๊ฒŒ ์—†์„ ๋•Œ๋ฅผ ๋Œ€๋น„ํ•ด ์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋„ฃ์–ด์ค„ ์ˆ˜๋„ ์žˆ์Œ
        }
        return list;
    }

    public void empSelectPrint(List<EmpVO> list) {
        for(EmpVO e : list) {
            System.out.println("์‚ฌ์›๋ฒˆํ˜ธ : " + e.getNo());
            System.out.println("์ด๋ฆ„ : " + e.getName());
            System.out.println("์ง์ฑ… : " + e.getJob());
            System.out.println("๋งค๋‹ˆ์ € : " + e.getMgr());
            System.out.println("์ž…์‚ฌ์ผ : " + e.getDate());
            System.out.println("๊ธ‰์—ฌ : " + e.getSal());
            System.out.println("๋ณด๋„ˆ์Šค : " + e.getComm());
            System.out.println("๋ถ€์„œ๋ฒˆํ˜ธ : " + e.getDeptNo());
            System.out.println("-----------------------------");
        }
    }

    public void empInsert() {
        System.out.println("์‚ฌ์› ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.");
        System.out.print("์‚ฌ์›๋ฒˆํ˜ธ(4์ž๋ฆฌ) : ");
        int no = sc.nextInt();
        System.out.print("์ด๋ฆ„ : ");
        String name = sc.next();
        System.out.print("์ง์ฑ… : ");
        String job = sc.next();
        System.out.print("๋งค๋‹ˆ์ €(4์ž๋ฆฌ) : ");
        int mgr = sc.nextInt();
        System.out.print("์ž…์‚ฌ์ผ : ");
        String date = sc.next();
        System.out.print("๊ธ‰์—ฌ : ");
        double sal = sc.nextDouble();
        System.out.print("๋ณด๋„ˆ์Šค : ");
        double comm = sc.nextDouble();
        System.out.print("๋ถ€์„œ๋ฒˆํ˜ธ : ");
        int deptNo = sc.nextInt();
        // INSERT INTO EMP VALUES(23,'ENAME', 'JOB',MGR,'HIREDATE',SAL,COMM,DEPTNO);
        String sql = "INSERT INTO EMP(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) VALUES(" +
                no + ", " + "'" + name + "'" + ", " + "'" + job + "'" + ", " + mgr + ", " + "'" + date + "'" +
                 ", " + sal + ", " + comm + ", " + deptNo + ")";

        try {
            conn = Common.getConnection();
            stmt = conn.createStatement();
            int ret = stmt.executeUpdate(sql); // ์˜ํ–ฅ๋ฐ›์€ ํ–‰์ด ๋ช‡ ๊ฐœ๋ƒ
            System.out.println("Return : " + ret);
        } catch(Exception e) {
            e.printStackTrace();
        }
        Common.close(stmt);
        Common.close(conn);
    }
}

EmpVO

  • ํ…Œ์ด๋ธ” ๊ฐ์ฒด ์ƒ์„ฑ
package com.kh.jdbc.vo;
import java.sql.Date; // sql์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ๊ณ  ๋ฐ›์„๊ฑฐ๋ผ sql date import ํ•ด์•ผํ•จ.
// DB์— ์žˆ๋Š” ์ •๋ณด๋ฅผ ํผ์™€์„œ ๊ฐ์ฒด์— ๋‹ด๋Š” ์šฉ๋„
public class EmpVO {
    private int no;
    private String name;
    private String job;
    private int mgr;
    private Date date;
    private double sal;
    private double comm;
    private int deptNo;

    public EmpVO(int no, String name, String job, int mgr, Date date, double sal, double comm, int deptNo) {
        this.no = no;
        this.name = name;
        this.job = job;
        this.mgr = mgr;
        this.date = date;
        this.sal = sal;
        this.comm = comm;
        this.deptNo = deptNo;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    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 getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    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;
    }
}

JDBCMain

package com.kh.jdbc;
import com.kh.jdbc.dao.EmpDAO;
import com.kh.jdbc.vo.EmpVO;

import java.util.List;
import java.util.Scanner;

public class JdbcMain {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        EmpDAO dao = new EmpDAO();
        while (true) {
            System.out.println("[EMP TABLE COMMAND]");
            System.out.println("๋ฉ”๋‰ด๋ฅผ ์„ ํƒํ•˜์„ธ์š”.");
            System.out.print("[1]SELECT [2]INSERT [3]UPDATE [4]DELETE [5]EXIT : ");
            int sel = sc.nextInt();
            switch(sel) {
                case 1 :
                    List<EmpVO> list = dao.empSelect();
                    dao.empSelectPrint(list);
                    break;
                case 2 :
                    dao.empInsert();
                    break;
                case 3 :
                    break;
                case 4 :
                    break;
                case 5 :
                    System.out.println("์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.");
                    return;
            }
        }
    }
}
profile

GiantStepDEV

@kongmi

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!