[JDBC] 클래스 생성부터 분리까지 모든 예제

Devel/JDBC|2020. 8. 19. 22:21
반응형

0. oracle_jar파일 buildPath에추가

1. 4가지 정보

        String driver = "oracle.jdbc.driver.OracleDriver";//6)j.jar파일의 api파일 로딩
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String userid = "scott";
        String passwd="tiger"

//2. 드라이버 생성
        Connection con = null;
        //Connetcion con = null;//db connect 객체
        //PreparedStatement pstmt = null;
        Statement stmt = null;//connect를 이용해 sql명령을 실행하는 객체
        ResultSet rs = null//sql실행 후 select결과를 저장하는 객체 
//2. 오라클 연결( Connection 연결)
            con = DriverManager.getConnection(url, userid, passwd);
            System.out.println("접속 성공");
        //3. sql 작성
 //5.Statement를 이용해 실행 select-executeQuery(),DML-executeUpdate()
        rs = stmt.executeQuery(sql);//Select 결과를 ResultSet으로 받음.
 //6. 자원반납 반대순서로 실행

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class JDBC_Test {
 
    public static void main(String[] args) {
        //0. oracle_jar파일 buildPath에추가
        //1. 4가지 정보
        String driver = "oracle.jdbc.driver.OracleDriver";//6)j.jar파일의 api파일 로딩
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String userid = "scott";
        String passwd="tiger";
        
        //2. 드라이버 생성
        Connection con = null;
        //Connetcion con = null;//db connect 객체
        //PreparedStatement pstmt = null;
        Statement stmt = null;//connect를 이용해 sql명령을 실행하는 객체
        ResultSet rs = null//sql실행 후 select결과를 저장하는 객체            
        
        try { 
            Class.forName(driver);//1. 드라이버 로딩
            System.out.println("드라이버 로딩 성공");
        //2. 오라클 연결( Connection 연결)
            con = DriverManager.getConnection(url, userid, passwd);
            System.out.println("접속 성공");
        //3. sql 작성
        String sql="select deptno, dname, loc from dept"// ;제거        
        
        //4. SQL 준비=>Statement, PreparedStatement, CallableStatment(PL/SQL)
        //connetcion에서 명령을 실행해줄 Statiement객체를 하나 얻어옴
        stmt = con.createStatement();
            
        //5.Statement를 이용해 실행 select-executeQuery(),DML-executeUpdate()
        rs = stmt.executeQuery(sql);//Select 결과를 ResultSet으로 받음.
        while(rs.next()) { //한레코드의 자료를 컬럼으로 접근 출력
            int deptno = rs.getInt("deptno"); //number값을 정수로 주면서 column이름을 줌.
            String dname = rs.getString("dname");
            String loc = rs.getString("loc");
            System.out.println(deptno +"\t"+dname+"\t"+loc);
        }
        }catch(ClassNotFoundException e) {
            e.printStackTrace();            
        }catch(SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null)rs.close();
                if(stmt != null)stmt.close();
                if(con != null)con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        //6. 자원반납 반대순서로 실행                     
    }//end main
}
// end class
 


-insertTest


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class InsertTest {
    public static void main(String[] args) {
        String driver = "oracle.jdbc.driver.OracleDriver";//6)j.jar파일의 api파일 로딩
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String userid = "scott";
        String passwd="tiger";
 
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;        
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url,userid,passwd);
            //4.sql작성
            int deptno=19;
            String deptname = "개발";
            String loc = "서울";
            String sql ="insert into dept (deptno, dname, loc)"+"values ("+deptno+", '"+deptname+"','"+loc+"')";            
            System.out.println(sql);    
            
            stmt = con.createStatement();
            int result = stmt.executeUpdate(sql);            
            System.out.println("실행된 레코드 갯수:"+result);
            
            String sql2 = "select * from dept";
            System.out.println(sql2);
            rs = stmt.executeQuery(sql2);
            
            while(rs.next()) {
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);
                System.out.println(deptno1 + "\t"+ dname1 + "\t"+ loc1);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch(SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null)rs.close();
                if(stmt !=null)stmt.close();
                if(con != null)con.close();                
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
 
}
 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class JDBC_Test02 {
 
    public static void main(String[] args) {
        String driver = "oracle.jdbc.driver.OracleDriver";//6)j.jar파일의 api파일 로딩
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String userid = "scott";
        String passwd="tiger";
        
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try {
            Class.forName(driver);
            System.out.println("드라이버 로딩 성공");
            con = DriverManager.getConnection(url, userid, passwd);
            System.out.println("접속 성공");
            String sql = "select deptno , dname, loc from dept";
            //String sql = "select deptno, dname, loc from dept";
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                int deptno = rs.getInt(1);
                String dname = rs.getString(2);
                String loc = rs.getString(3);
                System.out.println(deptno+"\t"+dname+"\t"+loc);
                
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch(SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if (rs != null)rs.close();
                if(stmt != null)stmt.close();
                if(con != null)con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
 
    }
 
}
 


-ClassforNameTest


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.lang.reflect.Field;
import java.lang.reflect.Method;
 
public class ClassforNameTest {
 
    public static void main(String[] args) throws Exception {
        Class cl = Class.forName("java.lang.Math");
        Field [] fiedls = cl.getDeclaredFields();
        for(Field f : fiedls) {
            System.out.println("fiedls: "+ f.getName());
        }
        Method [] methods = cl.getDeclaredMethods();
        for(Method m : methods) {
            System.out.println("methods: "+m.getName());
        }
 
    }
 
}
 
-Statement_update


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class Statement_update {
 
    public static void main(String[] args) {
        String driver = "oracle.jdbc.driver.OracleDriver";//6)j.jar파일의 api파일 로딩
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String userid = "scott";
        String passwd="tiger";
        
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url,userid,passwd);
            
            int deptno = 16;
            String dname = "영업";
            String loc = "제주";
            String sql = "Update dept set dname = '"+ dname+"', loc = '"+loc+"' where deptno ="+deptno ;
            System.out.println(sql);
            
            stmt = con.createStatement();
            int result = stmt.executeUpdate(sql);
//            rs = stmt.executeQuery(sql);
            System.out.println("실행된 레코드 갯수:"+result);
            
            String sql3 = "delete from dept where deptno ="+deptno;
            int result2 = stmt.executeUpdate(sql3);
            
            String sql2 = "select * from dept";
            rs = stmt.executeQuery(sql2);
            System.out.println(sql2);            
                        
            while (rs.next()) {
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);
                System.out.println(deptno1+"\t"+dname1+"\t"+loc1);                
                            }                        
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch(SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null)rs.close();
                if(stmt !=null)stmt.close();
                if(rs != null)rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }            
    }
 
}
 
-WhereTest


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class WhereTest {
 
    public static void main(String[] args) {
        String driver = "oracle.jdbc.driver.OracleDriver";//6)j.jar파일의 api파일 로딩
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String userid = "scott";
        String passwd="tiger";
        
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        
        try {
            Class.forName(driver);
            System.out.println("드라이버 로딩 성공");
            con = DriverManager.getConnection(url,userid,passwd);
            String name = "SALES";
            System.out.println("접속 성공");
            String sql = "select * from dept where dname='"+name+"'";
            stmt = con.createStatement();
            rs = stmt.executeQuery(sql);
            
            while(rs.next()) {
                int deptno = rs.getInt(1);
                String dname = rs.getString(2);
                String loc = rs.getString(3);
                System.out.println(deptno + "\t"+ dname + "\t"+loc);
            }
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }catch(SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(rs != null)rs.close();
                if(stmt != null)stmt.close();
                if(rs != null)rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
 
    }
 
}
 


-divide_Test


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class divide_Test {
    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 divide_Test() {
        //드라이버 로딩 
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url,userid,passwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }         
    
    public String getDeptAdata(int deptno) {
        //deptno를 select 하기 위한 sql문 작성
        // 한부서의 정보를 string으로 리턴 
        String sql="select deptno from dept";
        return sql;
    }
    public int delAdata(int deptno) {
        //부서번호를 가지고 부서 삭제 후 처리한 레코드 갯수 리턴 
        return 0;
    }
    public static void main(String[] args) {
       //객체생성
        //deptAdata 호출 검색 deptno전송 검색 결과 문자열로 받아서 출력
 
    }
 
}
 
-JDBCUpdate
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class JDBCUpdate {
 
    public static void main(String[] args) {
        String driver = "oracle.jdbc.driver.OracleDriver";//6)j.jar파일의 api파일 로딩
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String userid = "scott";
        String passwd="tiger";
        
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url,userid, passwd);
            
//            String sql = "update dept set dname=?,loc=? where deptno =?";
//            pstmt = con.prepareStatement(sql);
//            
//            pstmt.setString(1, "영업");
//            pstmt.setString(2, "제주");
//            pstmt.setInt(3, 90);
//            
//            int num = pstmt.executeUpdate();
//            System.out.println("실행된 레코드 갯수: "+num);
//            
//            String sql2 = "select * from dept where deptno = ?";
//            pstmt = con.prepareStatement(sql2);
//            pstmt.setInt(1, 90);
//            rs = pstmt.executeQuery();
//            System.out.println(sql2);
//
//            
//            String sql4 = "select deptno from dept where dname in(?,?)";
//            pstmt = con.prepareStatement(sql4);
//            pstmt.setString(1, "영업");
//            pstmt.setString(2, "개발");
//            rs = pstmt.executeQuery();
//            System.out.println(sql4);
            
//            String sql5 = "select * from dept order by deptno";
//            pstmt = con.prepareStatement(sql5);
//            rs = pstmt.executeQuery();
//            System.out.println(sql5);
            
            
            
//            String sql7 = "select loc from dept where deptno >= 20";
//            pstmt = con.prepareStatement(sql7);            
//            rs = pstmt.executeQuery();
//            System.out.println(sql7);    
            
//            String sql8 = "select dname, loc from dept where dname like 'A%'";
//            pstmt = con.prepareStatement(sql8);            
//            rs = pstmt.executeQuery();
//            System.out.println(sql8);
            
//            String sql9 = "select dname from dept where deptno = (select max(deptno) from dept)";
//            pstmt = con.prepareStatement(sql9);
//            rs = pstmt.executeQuery();
//            System.out.println(sql9);
            
//            String sql10 ="insert into dept (deptno, dname, loc)" + "values(?,?,?)";
//            pstmt = con.prepareStatement(sql10);
//            pstmt.setInt(1, 99);
//            pstmt.setString(2, "개발");
//            pstmt.setString(3, "서울");
//            rs = pstmt.executeQuery();
//            System.out.println(sql10);
            
//            String sql12 = "update dept set loc = '제주' where deptno =99";
//            pstmt = con.prepareStatement(sql12);
//            rs=pstmt.executeQuery();
//            System.out.println(sql12);
//            int num = pstmt.executeUpdate();
//            System.out.println("실행된 레코드 갯수: "+num);
            
//            String sql11 = "select deptno, loc from dept where deptno =99";
//            pstmt = con.prepareStatement(sql11);
//            rs = pstmt.executeQuery();
//            System.out.println(sql11);
            
            
            
//            while(rs.next()) {
//                int deptno1 = rs.getInt(1);
//                String dname1 = rs.getString(1);
//            String loc1 = rs.getString(2);
//                System.out.println(dname1);
                //+"\t"+dname1+"\t"+loc1
        
//        } 
        }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();
            }
        }
    
 
    }
 
}
 
-JDBC_Method_divide_Test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class JDBC_Method_divide_Test {
    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() {
        //드라이버 로딩 
        //connection 연결
        try {
            Class.forName(driver);
            System.out.println("로딩성공");
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
            
        
    }         
    
    public String deptAdata(int deptno) {
        //deptno를 select 하기 위한 sql문 작성
        // 한부서의 정보를 string으로 리턴 
        String data = null;
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            String sql="select deptno from dept where deptno =?";
            pstmt= con.prepareStatement(sql);
            pstmt.setInt(1,    deptno);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                int deptno1 = rs.getInt(1);
                String dname = rs.getString(2);
                String loc = rs.getString(3);
                data = deptno1+"  " + dname +"  "+ loc;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                rs.close();
                pstmt.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return data;
    }
        
    public int delAdata(int deptno) {
        //부서번호를 가지고 부서 삭제 후 처리한 레코드 갯수 리턴
        int x = 0;
        
        try {    
            con = DriverManager.getConnection(url, userid, passwd);
            String sql= "delete from dept where deptno=?";
            pstmt= con.prepareStatement(sql);
            pstmt.setInt(1, deptno);
             x= pstmt.executeUpdate();        
        
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                rs.close();
                pstmt.close();
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        }
        return x;    
    }
    public static void main(String[] args) {
       //객체생성
        //deptAdata 호출 검색 deptno전송 검색 결과 문자열로 받아서 출력
        JDBC_Method_divide_Test test= new JDBC_Method_divide_Test();
        System.out.println(test.deptAdata(90));
        System.out.println(test.delAdata(90));
 
    }
 
}
 

-JDBCTEST


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
public class JDBCTest {
 
    public static void main(String[] args) {
        String driver = "oracle.jdbc.driver.OracleDriver";//6)j.jar파일의 api파일 로딩
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String userid = "scott";
        String passwd="tiger";
        
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url,userid,passwd);
            String sql = "select deptno, dname, loc 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);
                System.out.println(deptno + "\t"+ dname +"\t"+ loc);
            }
            
        } 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();
            }
        }    
    }
}
 
-JDBCTEST2


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class JDBCTest2 {
    public static void main(String[] args) {
        String driver = "oracle.jdbc.driver.OracleDriver";//6)j.jar파일의 api파일 로딩
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        String userid = "scott";
        String passwd="tiger";
        
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            Class.forName(driver);
            con = DriverManager.getConnection(url,userid, passwd);
//            String sql = "insert into dept(deptno, dname, loc)"+
//            "values (?,?,?)";
//            pstmt = con.prepareStatement(sql);
//            pstmt.setInt(1, 14);
//            pstmt.setString(2, "개발");
//            pstmt.setString(3, "서울");
//            int num = pstmt.executeUpdate();
//            System.out.println("실행된 레코드 갯수: "+num);
            
            
            String sql2="select * from dept where deptno=?";
//            pstmt.setInt(1, 12);
            pstmt = con.prepareStatement(sql2);
            rs = pstmt.executeQuery(sql2);
            System.out.println(sql2);
            while(rs.next()) {
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);
                System.out.println(deptno1+"\t"+dname1+"\t"+loc1);
            }            
        } 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();
            }
        }
    }
}
 

-ArrayTest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class ArrayTest {
    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 ArrayTest() {
        try {
            Class.forName(driver);
            System.out.println("로딩 성공");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public ArrayList<String> getdeptAdata(int deptno) {
        ArrayList<String>list = new ArrayList<String>();
        try {
            con = DriverManager.getConnection(url, userid, passwd);
            String sql ="select deptno from dept where deptno = ?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, deptno);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                String result ="";
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);    
                result = deptno1 + "\t"+ dname1 + "\t"+ loc1;
                list.add(result);
                
            }
        } 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();
            }
    }
        return list;
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public ArrayList<String> getdeptAdata(int deptno) {
        ArrayList<String>list = new ArrayList<String>();
        try {
            con = DriverManager.getConnection(url, userid, passwd);
            String sql ="select deptno from dept where deptno = ?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, deptno);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                String result ="";
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);    
                result = deptno1 + "\t"+ dname1 + "\t"+ loc1;
                list.add(result);
                
            }
        } 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();
            }
    }
        return list;
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public ArrayList<String> searchDeptByName(String dname) {
        ArrayList<String> list = new ArrayList<String>();
        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()) {
                String result = "";
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);    
                result = deptno1 + "\t"+ dname1 + "\t"+ loc1;
                list.add(result);
            }
        } 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();
            }
        }
        return list;
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public ArrayList<DeptDTO> getAllDept() {
    ArrayList<DeptDTO>list = new ArrayList<DeptDTO>();
    
    try {
        con = DriverManager.getConnection(url,userid,passwd);
        String sql = "select * from dept";
        pstmt = con.prepareStatement(sql);
        rs = pstmt.executeQuery();
        while(rs.next()) {
            DeptDTO dept = new DeptDTO();
            dept.setDeptno(rs.getInt(1));
            dept.setDname(rs.getString(2));
            dept.setLoc(rs.getString(3));
            list.add(dept);
            
//            int deptno1 = rs.getInt(1);            
//            String dname1 = rs.getString(2);
//            String loc1 = rs.getString(3);    
//            result = deptno1 + "\t"+ dname1 + "\t"+ loc1;
//            list.add(result);
        }
    } 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();
        }
    }
    return list;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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 result ="";
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);    
                result = deptno1 + "\t"+ dname1 + "\t"+ loc1;
                list.add(result);
            }
        } 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();
            }
        }
        return list;
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public static void main(String[] args) {
        ArrayTest test= new ArrayTest();
        
        ArrayList<String> list = test.getAllDept();
//        ArrayList<String> list = test.searchDeptByName("개발");
//        ArrayList<String> list = test.getdeptAdata(50);
        for (String x : list) {
            System.out.println(x);
        }
 
        
        ArrayList<DeptDTO> list = test.getAllDept();
        for (DeptDTO deptDTO : list) {
            System.out.println(deptDTO.getDname());
        }
 
        
    }
 
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
 
public class ArrayTest {
    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 ArrayTest() {
        try {
            Class.forName(driver);
            System.out.println("로딩 성공");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public ArrayList<String> getdeptAdata(int deptno) {
        ArrayList<String>list = new ArrayList<String>();
        try {
            con = DriverManager.getConnection(url, userid, passwd);
            String sql ="select deptno from dept where deptno = ?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, deptno);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                String result ="";
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);    
                result = deptno1 + "\t"+ dname1 + "\t"+ loc1;
                list.add(result);
                
            }
        } 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();
            }
    }
        return list;
    }
    
    public ArrayList<String> getdeptAdata(int deptno) {
        ArrayList<String>list = new ArrayList<String>();
        try {
            con = DriverManager.getConnection(url, userid, passwd);
            String sql ="select deptno from dept where deptno = ?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, deptno);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                String result ="";
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);    
                result = deptno1 + "\t"+ dname1 + "\t"+ loc1;
                list.add(result);
                
            }
        } 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();
            }
    }
        return list;
    }
    public ArrayList<String> searchDeptByName(String dname) {
        ArrayList<String> list = new ArrayList<String>();
        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()) {
                String result = "";
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);    
                result = deptno1 + "\t"+ dname1 + "\t"+ loc1;
                list.add(result);
            }
        } 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();
            }
        }
        return list;
    }
    
    public ArrayList<DeptDTO> getAllDept() {
    ArrayList<DeptDTO>list = new ArrayList<DeptDTO>();
    
    try {
        con = DriverManager.getConnection(url,userid,passwd);
        String sql = "select * from dept";
        pstmt = con.prepareStatement(sql);
        rs = pstmt.executeQuery();
        while(rs.next()) {
            DeptDTO dept = new DeptDTO();
            dept.setDeptno(rs.getInt(1));
            dept.setDname(rs.getString(2));
            dept.setLoc(rs.getString(3));
            list.add(dept);
            
//            int deptno1 = rs.getInt(1);            
//            String dname1 = rs.getString(2);
//            String loc1 = rs.getString(3);    
//            result = deptno1 + "\t"+ dname1 + "\t"+ loc1;
//            list.add(result);
        }
    } 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();
        }
    }
    return list;
}
    
    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 result ="";
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);    
                result = deptno1 + "\t"+ dname1 + "\t"+ loc1;
                list.add(result);
            }
        } 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();
            }
        }
        return list;
    }
    
    public static void main(String[] args) {
        ArrayTest test= new ArrayTest();
        
        ArrayList<String> list = test.getAllDept();
//        ArrayList<String> list = test.searchDeptByName("개발");
//        ArrayList<String> list = test.getdeptAdata(50);
        for (String x : list) {
            System.out.println(x);
        }
 
        
        ArrayList<DeptDTO> list = test.getAllDept();
        for (DeptDTO deptDTO : list) {
            System.out.println(deptDTO.getDname());
        }
 
        
    }
 
}
 

-JDBC_Method_divide_Test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
public class JDBC_Method_divide_Test {
    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() {
        try {
            Class.forName(driver);
            System.out.println("로딩 성공");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
 
    public String search(String loc, String dname) {
    //지역만 넘어온 경우 지역만 검색 결과 리턴
    //이름만 넘오온 경우 이름만 검색 결과 리턴
    //이름, 지역이 넘어온 경우 두 가지를 이용 검색 결과 리턴
    //sql 하나로 result set 하나. 지역이름, null, or null, 지역이름.
        String result = "";
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            String sql="select * from dept where ";
            if(dname != null && loc == null) {
                sql += "dname = ?";    
                pstmt = con.prepareStatement(sql);            
                pstmt.setString(1, dname);
                
                
            }if(loc != null && dname == null) {
                sql += "loc =?";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, loc);
                
                
                
            }if(loc != null && dname !=null ) {
                sql += " loc =? and dname = ? ";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, loc);
                pstmt.setString(2, dname);
                
                
            }
            rs = pstmt.executeQuery();
            while(rs.next()) {
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);
                result = deptno1+"  " + dname1 +"  "+ loc1;
                System.out.println(result);
            }
 
        } 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();
        }}
        return result;
    }
    public String getAllDept(){
        //부서전체 select 
        //결과를 메인으로 리턴 메인에서 전체 데이터 출력 
        
        String result = "";
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            int deptno = 0;
            String dname =null;
            String loc =null;
            
            String sql = "select * from dept";
            pstmt = con.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);
                System.out.println(deptno1 + "\t"+ dname1 + "\t"+ loc1);
            }
        } 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();
            }
        }
        return result;
    }
    public  String searchDeptByLoc(String loc){
        //주소로 select 
        //검색한 내용을 ? 로 리턴 메인에서 전체 데이터 출력 
         String sql = "select * from dept where loc =?";
         String result = "";
         try {
            con = DriverManager.getConnection(url,userid,passwd);
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, loc);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                result = rs.getInt(1+ "   "+ rs.getString(2)+ "    "+rs.getString(3);
                System.out.println(result);
            }
        } 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();
            }
        }
         return result;
    }
    
    public  String searchDeptByName(String dname){
        //부서이름으로로 select 
        //검색한 내용을 ? 로 리턴 메인에서 전체 데이터 출력 
        //한부서당 한줄씩,,, 여러개 결과 나오면,,collection에 담아서 메인으로 리턴할지
        //String 한뒤 ArrayList로도 시도해보기
        String sql = "select * from dept where dname =?";
        String result ="";
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, dname);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                result = rs.getInt(1+ "   "+ rs.getString(2)+ "    "+rs.getString(3);
                System.out.println(result);
            }
        } 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();
            }
        }
        return result;}
    public ArrayList<String> searchDeptByName(String dname) {    
        ArrayList<String> list= new ArrayList<String>();
        try {
            con = DriverManager.getConnection(url, userid, passwd);            
            String sql = "select * from dept where ";
            sql = sql + "dname =? ";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, dname);            
            rs = pstmt.executeQuery();            
            while (rs.next()) {                
 
                String addResult= "";
                addResult += rs.getInt(1);
                addResult += rs.getString(2);
                addResult += rs.getString(3);                
                list.add(addResult);
            }
        } 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) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
    
        
        return list;
    }
    
        List<String> data = new ArrayList<String>();
        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 deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc = rs.getString(3);
                data.add(dname1);
                data.add(loc);                                                
            }            
        } 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();
            }
        }
        return data;
    }
    
    public int updateDept(String dname, String loc, int deptno){
        //부서번호로 dname, loc업데이트 후 
        //업데이트 결과 갯수 리턴
     int result = 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);
        
        result = pstmt.executeUpdate();
        System.out.println("실행된 레코드 갯수:" +result);
        
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        try {
//            rs.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
     return result;
    }
        
    
    public int insertDept(String dname, String loc, int deptno){
        //부서하나 추가후 
        //insert 결과 갯수 리턴
        int result = 0;
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            String sql="insert into dept(dname, loc, deptno)" + "values(?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, dname);
            pstmt.setString(2, loc);
            pstmt.setInt(3, deptno);
            
            result = pstmt.executeUpdate();    
            System.out.println("실행된 레코드 갯수:"+result);
            
                
        } 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();
            }
        }
    return result;
        
    }
    
    public String getdeptAdata(int deptno) {
        //부서번호로 select 
        //검색 결과가 없을 경우 사용자 정의 Exception 발생(RecordNotFoundException-메세지
        //찾는 부서 번호가 없습니다.
        //검색한 내용을 문자열로 리턴
        String data = null;
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            String sql = "select deptno from dept where deptno = ?";
            pstmt = con.prepareCall(sql);
            pstmt.setInt(1, deptno);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                int deptno1 = rs.getInt(1);
                String dname = rs.getString(2);
                String loc = rs.getString(3);
                data = deptno1+"  " + dname +"  "+ loc;                    
            }
        } catch (SQLException e) {
            System.out.println("찾는 부서 번호가 없습니다.");
            e.printStackTrace();
        }finally {
            try {
                rs.close();
                pstmt.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }return data;
            }
    public int updateDept(DeptDTO dept){
 
 int result = 0;
 try {
    con = DriverManager.getConnection(url,userid,passwd);
    String sql = "update dept set dname =? , loc=? where deptno =?";
    pstmt = con.prepareStatement(sql);
    pstmt.setString(1, dept.getDname());
    pstmt.setString(2, dept.getLoc());
    pstmt.setInt(3,dept.getDeptno());
    
    result = pstmt.executeUpdate();
    System.out.println("실행된 레코드 갯수:" +result);
    
catch (SQLException e) {
    e.printStackTrace();
}finally {
    try {
//        rs.close();
        pstmt.close();
        con.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 return result;
}
    public int insertDept(DeptDTO dept){
        int result = 0;
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            String sql="insert into dept(dname, loc, deptno)" + "values(?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, dept.getDname());
            pstmt.setString(2, dept.getLoc());
            pstmt.setInt(3, dept.getDeptno());            
            result = pstmt.executeUpdate();    
            System.out.println("실행된 레코드 갯수:"+result);
            
                
        } 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();
            }
        }
    return result;
        
    }
    public DeptDTO getdeptAdata(int deptno) {
        
        DeptDTO dept = null;
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            String sql = "select * from dept where deptno = ?";
            pstmt = con.prepareCall(sql);
            pstmt.setInt(1, deptno);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                dept = new DeptDTO(rs.getInt(1),rs.getString(2),rs.getString(3));
                
            }
        } catch (SQLException e) {
            System.out.println("찾는 부서 번호가 없습니다.");
            e.printStackTrace();
        }finally {
            try {
                if(rs!=null)rs.close();
                if(pstmt!=null)pstmt.close();
                if(pstmt!=null)con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }return dept;
            }
    
    public static void main(String[] args) {
        
        JDBC_Method_divide_Test test= new JDBC_Method_divide_Test();
//        DeptDTO list = test.getdeptAdata(90);
        System.out.println(test.getdeptAdata(50));
        
        //System.out.println(test.getdeptAdata(15));
        //System.out.println(test.delAdata(90));    }
 
}}
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
public class JDBC_Method_divide_Test {
    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() {
        try {
            Class.forName(driver);
            System.out.println("로딩 성공");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    public DeptDTO getdeptAdata(int deptno) {
        
        DeptDTO dept = null;
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            String sql = "select * from dept where deptno = ?";
            pstmt = con.prepareCall(sql);
            pstmt.setInt(1, deptno);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                dept = new DeptDTO(rs.getInt(1),rs.getString(2),rs.getString(3));
                
            }
        } catch (SQLException e) {
            System.out.println("찾는 부서 번호가 없습니다.");
            e.printStackTrace();
        }finally {
            try {
                if(rs!=null)rs.close();
                if(pstmt!=null)pstmt.close();
                if(pstmt!=null)con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }return dept;
            }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    public int insertDept(DeptDTO dept){
        int result = 0;
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            String sql="insert into dept(dname, loc, deptno)" + "values(?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, dept.getDname());
            pstmt.setString(2, dept.getLoc());
            pstmt.setInt(3, dept.getDeptno());            
            result = pstmt.executeUpdate();    
            System.out.println("실행된 레코드 갯수:"+result);
            
                
        } 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();
            }
        }
    return result;
        
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public int updateDept(DeptDTO dept){
 
 int result = 0;
 try {
    con = DriverManager.getConnection(url,userid,passwd);
    String sql = "update dept set dname =? , loc=? where deptno =?";
    pstmt = con.prepareStatement(sql);
    pstmt.setString(1, dept.getDname());
    pstmt.setString(2, dept.getLoc());
    pstmt.setInt(3,dept.getDeptno());
    
    result = pstmt.executeUpdate();
    System.out.println("실행된 레코드 갯수:" +result);
    
catch (SQLException e) {
    e.printStackTrace();
}finally {
    try {
//        rs.close();
        pstmt.close();
        con.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 return result;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public String getdeptAdata(int deptno) {
        //부서번호로 select 
        //검색 결과가 없을 경우 사용자 정의 Exception 발생(RecordNotFoundException-메세지
        //찾는 부서 번호가 없습니다.
        //검색한 내용을 문자열로 리턴
        String data = null;
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            String sql = "select deptno from dept where deptno = ?";
            pstmt = con.prepareCall(sql);
            pstmt.setInt(1, deptno);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                int deptno1 = rs.getInt(1);
                String dname = rs.getString(2);
                String loc = rs.getString(3);
                data = deptno1+"  " + dname +"  "+ loc;                    
            }
        } catch (SQLException e) {
            System.out.println("찾는 부서 번호가 없습니다.");
            e.printStackTrace();
        }finally {
            try {
                rs.close();
                pstmt.close();
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }return data;
            }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public int insertDept(String dname, String loc, int deptno){
        //부서하나 추가후 
        //insert 결과 갯수 리턴
        int result = 0;
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            String sql="insert into dept(dname, loc, deptno)" + "values(?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, dname);
            pstmt.setString(2, loc);
            pstmt.setInt(3, deptno);
            
            result = pstmt.executeUpdate();    
            System.out.println("실행된 레코드 갯수:"+result);
            
                
        } 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();
            }
        }
    return result;
        
    }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public int updateDept(String dname, String loc, int deptno){
        //부서번호로 dname, loc업데이트 후 
        //업데이트 결과 갯수 리턴
     int result = 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);
        
        result = pstmt.executeUpdate();
        System.out.println("실행된 레코드 갯수:" +result);
        
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        try {
//            rs.close();
            pstmt.close();
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
     return result;
    }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public  String searchDeptByName(String dname){
        //부서이름으로로 select 
        //검색한 내용을 ? 로 리턴 메인에서 전체 데이터 출력 
        //한부서당 한줄씩,,, 여러개 결과 나오면,,collection에 담아서 메인으로 리턴할지
        //String 한뒤 ArrayList로도 시도해보기
        String sql = "select * from dept where dname =?";
        String result ="";
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, dname);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                result = rs.getInt(1) + "   "+ rs.getString(2)+ "    "+rs.getString(3);
                System.out.println(result);
            }
        } 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();
            }
        }
        return result;}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public  String searchDeptByLoc(String loc){
        //주소로 select 
        //검색한 내용을 ? 로 리턴 메인에서 전체 데이터 출력 
         String sql = "select * from dept where loc =?";
         String result = "";
         try {
            con = DriverManager.getConnection(url,userid,passwd);
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, loc);
            rs = pstmt.executeQuery();
            while(rs.next()) {
                result = rs.getInt(1+ "   "+ rs.getString(2)+ "    "+rs.getString(3);
                System.out.println(result);
            }
        } 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();
            }
        }
         return result;
    }



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public String search(String loc, String dname) {
    //지역만 넘어온 경우 지역만 검색 결과 리턴
    //이름만 넘오온 경우 이름만 검색 결과 리턴
    //이름, 지역이 넘어온 경우 두 가지를 이용 검색 결과 리턴
    //sql 하나로 result set 하나. 지역이름, null, or null, 지역이름.
        String result = "";
        try {
            con = DriverManager.getConnection(url,userid,passwd);
            String sql="select * from dept where ";
            if(dname != null && loc == null) {
                sql += "dname = ?";    
                pstmt = con.prepareStatement(sql);            
                pstmt.setString(1, dname);
                
                
            }if(loc != null && dname == null) {
                sql += "loc =?";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, loc);
                
                
                
            }if(loc != null && dname !=null ) {
                sql += " loc =? and dname = ? ";
                pstmt = con.prepareStatement(sql);
                pstmt.setString(1, loc);
                pstmt.setString(2, dname);
                
                
            }
            rs = pstmt.executeQuery();
            while(rs.next()) {
                int deptno1 = rs.getInt(1);
                String dname1 = rs.getString(2);
                String loc1 = rs.getString(3);
                result = deptno1+"  " + dname1 +"  "+ loc1;
                System.out.println(result);
            }
 
        } 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();
        }}
        return result;
    }


-workshop


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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 workshop {
 
    public static void main(String[] args) throws ClassNotFoundException {
        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.println("매출 조회 메뉴 = [ 매출 일자 순 :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 (SQLException e) {
            
            e.printStackTrace();
        }catch(ClassNotFoundException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if( rs != null) rs.close();
                if( pstmt != null) pstmt.close();
                if( con != null) con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        }}


댓글()
loading