ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA ์ž๋ฐ” JDBC ๋ฌธ์ œ ์—ฐ์Šต
    JAVA 2020. 8. 19. 22:41

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

     

     

     

     

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

    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    String userid = "scott";
    String passwd = "tiger";
    
    Connection con= null;
    ResultSet rs= null;
    PreparedStatement pstmt= null;
    public JDBC_Method_divide_Test() throws ClassNotFoundException, SQLException {
            Class.forName(driver);
    }

    ์—ฐ๋™์„ ์œ„ํ•œ ๊ธฐ๋ณธ ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋กœ๋”ฉํ•œ๋‹ค.

     

     

    1. search ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค๊ธฐ

    public String search(String loc, String name) {
        //์ง€์—ญ๋งŒ ๋„˜์–ด์˜จ ๊ฒฝ์šฐ ์ง€์—ญ๋งŒ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๋ฆฌํ„ด
        //์ด๋ฆ„๋งŒ ๋„˜์˜ค์˜จ ๊ฒฝ์šฐ ์ด๋ฆ„๋งŒ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๋ฆฌํ„ด
        //์ด๋ฆ„, ์ง€์—ญ์ด ๋„˜์–ด์˜จ ๊ฒฝ์šฐ ๋‘ ๊ฐ€์ง€๋ฅผ ์ด์šฉ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ๋ฆฌํ„ด
    }

    ์œ„ ์„ธ๊ฐ€์ง€ ์ƒํ™ฉ์— ๋Œ€ํ•œ ์กฐ๊ฑด๋ฌธ์„ ์ฑ„์›Œ์•ผ ํ•œ๋‹ค. searchํ•จ์ˆ˜๋Š” ์ธ์ž๋กœ ์ง€์—ญ๊ณผ ์ด๋ฆ„์„ ๋ฐ›๋Š”๋‹ค.

     

    #1 ์ฒซ๋ฒˆ์งธ if
    
    public String search(String loc, String dname) {
            String data = null;
            try {
                con = DriverManager.getConnection(url, userid, passwd);
    
                if(dname==null && loc != null) {
                    String sql = "select loc from dept where loc = ?";
                    pstmt = con.prepareStatement(sql);
                    pstmt.setString(1, loc);
                    rs = pstmt.executeQuery();
                    while(rs.next()) {
                        String loc1 = rs.getString(1);
                        data = loc1;
                    }

    ์ง€์—ญ๊ฐ’๋งŒ ๋ฐ›์€ ๊ฒฝ์šฐ ๋ฐ›์€ ์ง€์—ญ์— ๋Œ€ํ•œ ๊ฒ€์ƒ‰๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” sql๋ฌธ์„ ์ž‘์„ฑํ•œ๋‹ค.

    #2 ๋‘๋ฒˆ์งธ else if
    
    }else if(loc == null && dname !=null) {
                    String sql = "select dname from dept where dname = ?";
                    pstmt = con.prepareStatement(sql);
                    pstmt.setString(1, dname);
                    rs = pstmt.executeQuery();
                    while(rs.next()) {
                        String dname1 = rs.getString("dname");
                        data = dname1;
                    }

    ์ด๋ฆ„๋งŒ ๋ฐ›์€ ๊ฒฝ์šฐ ๋ฐ›์€ ํ•ด๋‹น ์ด๋ฆ„์— ๋Œ€ํ•œ ๊ฒ€์ƒ‰๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

    #3 else 
    
    }else {
                    String sql = "select loc, dname from dept where loc=? and dname=?";
                    pstmt = con.prepareStatement(sql);
                    pstmt.setString(1, loc);
                    pstmt.setString(2, dname);
                    rs = pstmt.executeQuery();
                    while(rs.next()) {
                        String loc1 = rs.getString("loc");
                        String dname1 = rs.getString("dname");
                    //    System.out.println(loc1+"\t"+dname1);
                        data = loc1 +"\t"+ dname1;
                    }
                }

    ์ด๋ฆ„๊ณผ ์ง€์—ญ ๋‘˜ ๋‹ค ๋ฐ›์€ ๊ฒฝ์šฐ ๋‘ ์ •๋ณด ๋ชจ๋‘ ๋ฝ‘์•„์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

    #์˜ˆ์™ธ์žก๊ธฐ
    
    }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.getStackTrace();
                }
            }return data;
        }

    try๋ฌธ์ด ๋๋‚ฌ์œผ๋‹ˆ ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค. ์‹คํ–‰๋œ ์—ญ์ˆœ์œผ๋กœ ์ž์›์„ ๋ฐ˜๋‚ฉํ•˜๊ณ  ์ตœ์ข…๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

    # ๋ฉ”์ธํ•จ์ˆ˜์— ํ”„๋ฆฐํŠธ
    
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
            JDBC_Method_divide_Test test= new JDBC_Method_divide_Test();
            String result1 = test.search("์ œ์ฃผ", null);
            String result2 = test.search(null, "์˜์—…");
            String result3 =test.search("์ œ์ฃผ", "์˜์—…");
            System.out.println(result1);
            System.out.println(result2);
            System.out.println(result3);

    ์ธ์ž๋ฆฌ์ŠคํŠธ๋ฅผ ๋งž์ถฐ์„œ ์„ธ๊ฐ€์ง€ ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    2. getAllDept ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค๊ธฐ

    public String  getAllDept(){
        //๋ถ€์„œ์ „์ฒด select 
        //๊ฒฐ๊ณผ๋ฅผ ๋ฉ”์ธ์œผ๋กœ ๋ฆฌํ„ด ๋ฉ”์ธ์—์„œ ์ „์ฒด ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ 
    }

    deptํ…Œ์ด๋ธ”์˜ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.

     

    public String  getAllDept(){
            String data = "";
            try {
                con = DriverManager.getConnection(url, userid, passwd);
                String sql = "select * from dept";
                pstmt = con.prepareStatement(sql);
                rs = pstmt.executeQuery();
    
                while (rs.next()) {
                    int deptno = rs.getInt(1);
                    String dname = rs.getString(2);
                    String loc = rs.getString(3);
                    data += deptno + "\t" + dname + "\t" + loc +"\n";
                }
            } 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.getStackTrace();
                }
            }return data;
    #๋ฉ”์ธ
    
    System.out.println(test.getAllDept());

    ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฉ”์ธ์—์„œ ํ˜ธ์ถœํ•œ๋‹ค.

     

    # ArrayList ์‚ฌ์šฉ
    
    public ArrayList<String>  getAllDept(){
            ArrayList<String> list = new ArrayList<String>();
            try {
                con = DriverManager.getConnection(url, userid, passwd);
                String sql = "select * from dept";
                pstmt = con.prepareStatement(sql);
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    String data = "";
                    data += rs.getInt(1);
                    data += rs.getString(2);
                    data += rs.getString(3);
                    data += "\n";
                    list.add(data);
                }
            } 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.getStackTrace();
                }
            }return list;
        }
    ArrayList<String> list = test.getAllDept();
            for(String x : list) {
                System.out.println(x);
            }

    ArrayList๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด๋ณธ๋‹ค. selectํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค. ๋ฉ”์ธํ•จ์ˆ˜ ์•ˆ์—์„œ ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด ๋ฆฌ์ŠคํŠธ์˜ ์ „์ฒด ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    3. searchDeptByLoc ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค๊ธฐ

    public  String searchDeptByLoc(String loc){
        //์ฃผ์†Œ๋กœ select 
        //๊ฒ€์ƒ‰ํ•œ ๋‚ด์šฉ์„ ? ๋กœ ๋ฆฌํ„ด ๋ฉ”์ธ์—์„œ ์ „์ฒด ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ 
    }

    ์ง€์—ญ๊ฐ’์„ ๋ฐ›์•„์„œ ๊ฒ€์ƒ‰๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.

     

    public  String searchDeptByLoc(String loc){
            String data = "";
            try {
                con = DriverManager.getConnection(url, userid, passwd);
                String sql = "select * from dept where loc=?";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, loc);
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    int deptno = rs.getInt(1);
                    String dname = rs.getString(2);
                    String loc1 = rs.getString(3);
                    data += deptno + "\t" + dname + "\t" + loc1 +"\n";
                }    
            } 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.getStackTrace();
                }
            }
            return data;
        }
    System.out.println(test.searchDeptByLoc("์ œ์ฃผ"));

     

     

    4. searchDeptByName ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค๊ธฐ

    public  String searchDeptByName(String dname){
        //๋ถ€์„œ์ด๋ฆ„์œผ๋กœ๋กœ select 
        //๊ฒ€์ƒ‰ํ•œ ๋‚ด์šฉ์„ ? ๋กœ ๋ฆฌํ„ด ๋ฉ”์ธ์—์„œ ์ „์ฒด ๋ฐ์ดํ„ฐ ์ถœ๋ ฅ 
    }

    ๋ถ€์„œ ์ด๋ฆ„์œผ๋กœ ๋ฐ›์•„์„œ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.

     

    public  String searchDeptByName(String dname){
            String data = "";
            try {
                con = DriverManager.getConnection(url, userid, passwd);
                String sql = "select * from dept where dname=?";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, dname);
                rs = pstmt.executeQuery();
                while (rs.next()) {
                    int deptno = rs.getInt(1);
                    String dname1 = rs.getString(2);
                    String loc1 = rs.getString(3);
                    data += deptno + "\t" + dname1 + "\t" + loc1 +"\n";
                }    
            } 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.getStackTrace();
                }
            }
            return data;
        }
    System.out.println(test.searchDeptByName("๊ฐœ๋ฐœ"));

     

     

    5. updateDept ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค๊ธฐ

    public int updateDept(String dname, String loc, int deptno){
        //๋ถ€์„œ๋ฒˆํ˜ธ๋กœ dname, loc์—…๋ฐ์ดํŠธ ํ›„ 
        //์—…๋ฐ์ดํŠธ ๊ฒฐ๊ณผ ๊ฐฏ์ˆ˜ ๋ฆฌํ„ด
    }

    ํŠน์ • ๋ถ€์„œ๋ฒˆํ˜ธ์— ํ•ด๋‹นํ•˜๋Š” ๋ถ€์„œ๋ช…๊ณผ ์ง€์—ญ์„ updateํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค.

     

    public int updateDept(String dname, String loc, int deptno){
            int num = 0;
            try {
                con = DriverManager.getConnection(url, userid, passwd);
                String sql = "update dept set dname=?, loc=? where deptno=?";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, dname);
                pstmt.setString(2, loc);
                pstmt.setInt(3, deptno);
                num = pstmt.executeUpdate();
            } 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.getStackTrace();
                }
            }return num;
        }

    selec๋ฌธ๊ณผ ๋‹ค๋ฅด๊ฒŒ ์‹คํ–‰๊ฒฐ๊ณผ ๊ฐฏ์ˆ˜๋งŒ ๋ฆฌํ„ดํ•œ๋‹ค.

    System.out.println(test.updateDept("์˜์—…", "ํŒ๊ต", 80));

     

     

    6. insertDept ๋ฉ”์†Œ๋“œ ๋งŒ๋“ค๊ธฐ

    public int insertDept(String dname, String loc, int deptno){
        //๋ถ€์„œํ•˜๋‚˜ ์ถ”๊ฐ€ํ›„ 
        //insert ๊ฒฐ๊ณผ ๊ฐฏ์ˆ˜ ๋ฆฌํ„ด
    }

    ๋ถ€์„œ๋ฒˆํ˜ธ์™€ ๋ถ€์„œ๋ช…, ์ง€์—ญ๋ช…์„ ๋ฐ›์•„์„œ ์ƒˆ๋กœ์šด ๋ ˆ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ ๋‹ค.

     

    public int insertDept(String dname, String loc, int deptno){
            int num = 0;
            try {
                con = DriverManager.getConnection(url, userid, passwd);
                String sql = "insert into dept (deptno, dname, loc) values(?,?,?)";
                pstmt = con.prepareStatement(sql);
                pstmt.setInt(1, deptno);
                pstmt.setString(2, dname);
                pstmt.setString(3, loc);
                num = pstmt.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally  {
                try {
                    if(pstmt!=null)pstmt.close();
                    if(con!=null)con.close();
                }catch(SQLException e) {
                    e.getStackTrace();
                }
            }return num;
        }
    System.out.println(test.insertDept("๊ฒฝ์˜", "๊ฐ•๋‚จ", 88));

     

     

     

    JDBC Dept DTO์—ฐ์Šต

    //  Deptํ…Œ์ด๋ธ”์˜ ํ•˜๋‚˜์˜ ๋ ˆ์ฝ”๋“œ ์ €์žฅ์šฉ๋„
    
    package com.dto;
    
    public class Dept {
    
        private int deptno;
        private String dname;
        private String loc;
    
        public Dept() {}
    
        public Dept(int deptno, String dname, String loc) {
            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;
        }
    
        @Override
        public String toString() {
            return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
        }
    
    }

    Deptํ…Œ์ด๋ธ”์„ ํ•˜๋‚˜์˜ ๋ชจ๋ธ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ ๋‹ค.

    //์‚ฌ์šฉ์ž์ •์˜ ์˜ˆ์™ธํด๋ž˜์Šค
    
    package com.exception;
    
    public class RecordNotFoundException extends Exception {
        public RecordNotFoundException(String mesg) {
            super(mesg);
            }
    }

    ์˜ˆ์™ธํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ ๋‹ค.

    package com.service;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.util.ArrayList;
    
    import com.dao.OracleTxDAO;
    import com.dto.Dept;
    import com.exception.RecordNotFoundException;
    
    public class OracleTxService {
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String userid = "scott";
        String passwd = "tiger";
    
        OracleTxDAO dao;
    
        public OracleTxService() {
            dao = new OracleTxDAO(); // ์ „์ฒด์—์„œ ์จ์•ผํ•˜๋‹ˆ๊นŒ ๋ฉค๋ฒ„๋ณ€์ˆ˜๋กœ ์ƒ์„ฑ
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }// end OracleDAO
    
        public ArrayList<Dept> select() throws SQLException {
            Connection con = null;
            ArrayList<Dept> list = null;
            try {
                con = DriverManager.getConnection(url, userid, passwd);
                list = dao.select(con);  //์ปค๋„ฅ์…˜์„ ์„œ๋น„์Šค์—์„œ ์ฒ˜๋ฆฌํ•˜๊ธฐ๋กœ ํ–ˆ์œผ๋‹ˆ๊นŒ ์ฒ˜๋ฆฌ๋œ๊ฑฐ dao๋กœ ๋ณด๋‚ด
            } finally {  //์—ฌ๊ธฐ์„œ๋Š” ์˜ˆ์™ธ์•ˆ์žก์Œ. ์ž์›๋ฐ˜๋‚ฉ๋งŒ ์ฒ˜๋ฆฌ.
                if (con != null)
                    con.close();
            }
            return list;
        }// end select
    
        public void insert(Dept xx) {
            Connection con = null;
            try {
                con = DriverManager.getConnection(url, userid, passwd);
                dao.insert(con, xx);
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            } finally {
                try {
                    if (con != null)
                        con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }// end insert
    
        public void update(Dept xx2) throws RecordNotFoundException {
            Connection con = null;
            try {
                con = DriverManager.getConnection(url, userid, passwd);
                dao.update(con, xx2);
            } catch (SQLException e) {
                System.out.println(e.getMessage());
            } finally {
                try {
                    if (con != null)
                        con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }// end update
    
        public void delete(int i) {
            Connection con = null;
            try {
                con = DriverManager.getConnection(url, userid, passwd);
                dao.delete(con, i);
            } catch (SQLException e) {
                System.out.println(e.getMessage());
                // e.printStackTrace();
            } finally {
                try {
                    if (con != null)
                        con.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }// end deletes
    
    }

    ์„œ๋น„์Šค ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค. ์„œ๋น„์Šค ํด๋ž˜์Šค์—์„œ๋Š” ์ฃผ๋กœ ์ปค๋„ฅ์…˜ ์ฒ˜๋ฆฌ๋ฅผ ๋‹ด๋‹นํ•œ๋‹ค.

    package com.dao;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    
    import com.dto.Dept;
    import com.exception.RecordNotFoundException;
    
    public class OracleTxDAO {
        String driver = "oracle.jdbc.driver.OracleDriver";
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String userid = "scott";
        String passwd = "tiger";
    
        public void update(Connection con, Dept xx) throws RecordNotFoundException {
            PreparedStatement pstmt = null;
            try {
                String sql = "update dept set " + " dname=? , loc=?  where deptno=?";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, xx.getDname());
                pstmt.setString(2, xx.getLoc());
                pstmt.setInt(3, xx.getDeptno());
                int num = pstmt.executeUpdate();
                System.out.println("์‹คํ–‰๋œ ๋ ˆ์ฝ”๋“œ ๊ฐฏ์ˆ˜:" + num);
                if (num == 0) {
                    throw new RecordNotFoundException(xx.getDeptno() + " ๋ ˆ์ฝ”๋“œ์—†๋‹ค.");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (pstmt != null)
                        pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }// end upate
    
        public void delete(Connection con, int i) {
            PreparedStatement pstmt = null;
            try {
                String sql = "delete from dept where deptno=?";
                pstmt = con.prepareStatement(sql);
                pstmt.setInt(1, i);
                int num = pstmt.executeUpdate();
                System.out.println("์‹คํ–‰๋œ ๋ ˆ์ฝ”๋“œ ๊ฐฏ์ˆ˜:" + num);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (pstmt != null)
                        pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    
        public void insert(Connection con, Dept xx) {
            PreparedStatement pstmt = null;
            try {
                String sql = "insert into dept (deptno,dname,loc)" + " values (?,?,? )";
                pstmt = con.prepareStatement(sql);
                pstmt.setInt(1, xx.getDeptno());
                pstmt.setString(2, xx.getDname());
                pstmt.setString(3, xx.getLoc());
                int num = pstmt.executeUpdate();
                System.out.println("์‹คํ–‰๋œ ๋ ˆ์ฝ”๋“œ ๊ฐฏ์ˆ˜:" + num);
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (pstmt != null)
                        pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }// end insert
    
        public ArrayList<Dept> select(Connection con) throws SQLException {
            ArrayList<Dept> list = new ArrayList<Dept>();
            PreparedStatement pstmt = null;
            ResultSet rs = null;
            String sql = "select deptno x,dname,loc from dept ";
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                int deptno = rs.getInt("x");
                String dname = rs.getString(2);
                String loc = rs.getString("loc");
                Dept dto = new Dept(deptno, dname, loc);
                list.add(dto);
            }
            if (rs != null)
                rs.close();
            if (pstmt != null)
                pstmt.close();
    
            return list;
        }// end select
    }

    DAOํด๋ž˜์Šค์—์„  ์„œ๋น„์Šค ํด๋ž˜์Šค์—์„œ ๋ฐ›์•„์˜จ ์ปค๋„ฅ์…˜์„ ์‚ฌ์šฉํ•ด ๊ฐ ํ•จ์ˆ˜์— ํ•ด๋‹นํ•˜๋Š” SQL๋ฌธ์„ ์ž‘์„ฑํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฉ”์ธ์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

    //main
    
    import java.sql.SQLException;
    import java.util.ArrayList;
    
    import com.dto.Dept;
    import com.exception.RecordNotFoundException;
    import com.service.OracleTxService;
    
    public class OracleTxMain {
    
        public static void main(String[] args) {
            OracleTxService service = new OracleTxService();
    
            //4๋ฒˆ
            service.delete(99);
    
            // 3๋ฒˆ
            Dept xx2 = new Dept(99, "์˜์—…", "์„œ์šธ");
            try {
                service.update(xx2);
            } catch (RecordNotFoundException e1) {
                System.out.println(e1.getMessage());
            }
    
            // 2๋ฒˆ
            int deptno = 99;
            String dname = "๊ฐœ๋ฐœ";
            String loc = "์ œ์ฃผ";
            Dept xx = new Dept(deptno, dname, loc);
            service.insert(xx);
            
            // 1๋ฒˆ
            try {
                ArrayList<Dept> list = service.select();
                for (Dept dept : list) {
                    System.out.println(dept);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    ๋ฉ”์ธ์—์„œ ์„œ๋น„์Šค ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    select()ํ•จ์ˆ˜๋งŒ ํ•˜๋‚˜ํ•˜๋‚˜ ๋œฏ์–ด๋ณด๊ธฐ

    public OracleTxService() {
            dao = new OracleTxDAO();
            try {
                Class.forName(driver);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }// end OracleDAO
        }

    ์„œ๋น„์Šค ํด๋ž˜์Šค์—์„œ ์ƒ์„ฑ์ž๋ฅผ ๋จผ์ € ๋งŒ๋“ ๋‹ค. ์„œ๋น„์Šค ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋ฉด์„œ dao ํด๋ž˜์Šค๋„ ์“ธ ์ˆ˜ ์žˆ๊ฒŒ ๊ฐ์ฒด์ƒ์„ฑ ํ•ด์ฃผ๊ณ  ๋“œ๋ผ์ด๋ฒ„๋ฅผ ๋กœ๋”ฉํ•œ๋‹ค.

    public ArrayList<Dept> select() throws SQLException {
            //db์—ฐ๊ฒฐ
            Connection con = null;
            ArrayList<Dept> list = null;
            try {
                con = DriverManager.getConnection(url, userid, passwd);
                list = dao.select(con);
            } finally {
                if(con!=null)con.close();
            }
            return list;
        }// end select

    select ํ•จ์ˆ˜ ์•ˆ์—์„œ ์ปค๋„ฅ์…˜ ์—ฐ๊ฒฐํ•ด์ฃผ๊ณ , ์–ด๋ ˆ์ด๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค. ๋ฆฌ์ŠคํŠธ์— dao์˜ ์…€๋ ‰ํŠธํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ปค๋„ฅ์…˜ ์—ฐ๊ฒฐ์„ ๋‹ด์•„์„œ ๋ณด๋‚ด์ฃผ๊ณ  ์ž์›๋ฐ˜๋‚ฉ ํ•ด์ฃผ๊ณ  ๋ฆฌ์ŠคํŠธ ๋ฆฌํ„ดํ•˜๊ณ  ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค.

    ์ด์ œ dao๋กœ ๊ฐ„๋‹ค.

    public ArrayList<Dept> select(Connection con)throws SQLException {
        ArrayList<Dept> list = new ArrayList<Dept>();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = "select deptno x, dname, loc from dept";
        pstmt = con.prepareStatement(sql);
        rs = pstmt.executeQuery();
        while(rs.next()) {
            int deptno = rs.getInt("x");
            String dname = rs.getString("dname");
            String loc = rs.getString(3);
            Dept dto = new Dept(deptno, dname, loc);
            list.add(dto);
        }if(rs != null) rs.close();
        if(pstmt!=null) pstmt.close();
        return list;
        }//end select
    }

    ์…€๋ ‰ํ•จ์ˆ˜ ์ธ์ž๋กœ  con์„ ๋ฐ›๋Š”๋‹ค. ๋จผ์ € ์–ด๋ ˆ์ด๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด ์ƒ์„ฑํ•ด์ฃผ๊ณ  con์€ ๋ฐ›์•„์™”์œผ๋‹ˆ๊นŒ ๋‚˜๋จธ์ง€ pstmt, rs๋ฅผ ์„ธํŒ…ํ•œ๋‹ค. ์‹œํ€„๋ฌธ ์ž‘์„ฑํ•˜๊ณ , pstmt๋กœ ์ฟผ๋ฆฌ๋ฌธ ์‹คํ–‰ํ•˜๊ณ , ์‹คํ–‰๋œ ๊ฑธ rs์— ์ €์žฅํ•ด์ฃผ๊ณ , ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ ค์„œ Deptํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•ด์„œ dto๋ผ๋Š” ๋ณ€์ˆ˜์— ๋„ฃ์–ด์ค€๋‹ค. ๊ทธ๋ ‡๊ฒŒ ๋ถˆ๋Ÿฌ์˜จ ์ •๋ณด๋ฅผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ ํ›„ ์ž์›๋ฐ˜๋‚ฉํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ ๋ฆฌํ„ดํ•˜๊ณ  ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค. ์ด์ œ ๋ฆฌํ„ด๋œ ๋ฆฌ์ŠคํŠธ๋Š” ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ์„œ๋น„์Šค ํด๋ž˜์Šค๋กœ ๊ฐ”๋‹ค๊ฐ€ ๋ฉ”์ธ์œผ๋กœ ๊ฐ€๊ฒŒ๋œ๋‹ค.

    ์ด์ œ ๋ฉ”์ธ์œผ๋กœ ๊ฐ„๋‹ค.

    OracleTxService service = new OracleTxService();

    ๋ฉ”์ธ์—์„œ๋Š” ์„œ๋น„์Šค ํด๋ž˜์Šค๋งŒ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๋‹ˆ๊นŒ ์„œ๋น„์Šค ๊ฐ์ฒด๋ฅผ ๋จผ์ € ์ƒ์„ฑํ•œ๋‹ค.

    try {
                ArrayList<Dept> list = service.select();
                for(Dept dept : list) {
                    System.out.println(dept);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

    ์ด์ œ ์–ด๋ ˆ์ด๋ฆฌ์ŠคํŠธ ํƒ€์ž…์œผ๋กœ ์„œ๋น„์Šค์˜ ์…€๋ ‰ํŠธํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์ฃผ๊ณ  ๋ฐ›์•„์˜จ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ ค์„œ ๋‹ค ์ถœ๋ ฅํ•œ๋‹ค. ์˜ˆ์™ธ๋ฅผ ์žก๊ณ  ์…€๋ ‰ํŠธ ํ•จ์ˆ˜ ์‹คํ–‰์€ ๋๋‚œ๋‹ค. ๋ฉ”์ธ์—์„œ๋Š” ํ•จ์ˆ˜ ํ˜ธ์ถœ๋งŒ ํ•˜๊ณ  ์ž์›๋ฐ˜๋‚ฉ์€ ์„œ๋น„์Šค๋ž‘ dao์—์„œ ๋‹ค ์ฒ˜๋ฆฌํ•ด์คฌ๊ธฐ๋•Œ๋ฌธ์— ์˜ˆ์™ธ๋งŒ ์žก๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ๋๋‚œ๋‹ค.

     

     

     

    ๐Ÿ˜œ NEXT : JDBC ๋ฌธ์ œ ๋˜ ์—ฐ์Šตํ•ด๋ณด๊ธฐ

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

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