executeQuery(String sql) executeUpdate(String sql)
반환값이 있는 경우 반환값이 없는 경우
select insert, delete, update

 

  • if (repeat.equalsIgnoreCase("y") {
  • System.exit(0);
  • Connection conn=connection();
  • Statement stmt = conn.createStatement();
  • ResultSet rs = stmt.executQuery(sql);
  • int gno=rs.getInt("gno");
  • conn.close();
  • String sql = "update gift set gname= '" + gname + "' where gno=" + num;
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
package ex02_jdbc_Statement;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
 
//Gift table CRUD
//.close() 닫는것만 있는 method생성  
//update세분화하기(+switch case)
public class GiftCRUD {
 
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        System.out.println("<<<<<<<CRUD PROGRAM>>>>>>");
 
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("원하는 기능의 번호를 입력해주세요.(select:1, insert:2, updaet:3, delete:4)=>");
            int num = sc.nextInt();
 
            switch (num) {
            case 1:
                select();
                break;
            case 2:
                insert();
                break;
            case 3:
                update();
                break;
            case 4:
                delete();
                break;
            default:
                System.out.print("잘못 입력하셨습니다. 다시 입력해주세요.\n");
                continue;
            } // switch end
 
            System.out.print("프로그램을 계속하시겠습니까?(y/n)=>");
            String repeat = sc.next();
            if (repeat.equalsIgnoreCase("y")) {
                continue;
            } else {
                System.out.println("CRUD PROGRAM을 종료합니다.");
                System.exit(0);
            } // if end
        } // while end
 
    }
 
    // 연결
    public static Connection connection() throws ClassNotFoundException, SQLException {
 
        // 1.Driver Load - exception발생
        Class.forName("oracle.jdbc.OracleDriver"); // 방법2
 
        // 2.Connection & Open
        String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
        String uid = "db명";
        String pwd = "비밀번호";
 
        Connection conn = DriverManager.getConnection(url, uid, pwd); // ==>예외발생
        return conn;
    }
 
    
    // SELECT
    public static void select() throws ClassNotFoundException, SQLException {
        Connection conn = connection();
        System.out.println("<<<SELECT>>>");
 
        // 3.사용(DML 명령어 - select)
        Statement stmt = conn.createStatement();
        String sql = "SELECT * FROM GIFT"// 소문자->대문자로 ==> ctrl+shift+x
        ResultSet rs = stmt.executeQuery(sql);
 
        System.out.println("상품번호 \t상품명 \t최저가 \t최고가");
        while (rs.next()) { // rs의 다음요소가 있다면
            int gno = rs.getInt("gno"); // 방법1-index번호사용
            String gname = rs.getNString("gname"); // 방법2-필드명사용
            int g_s = rs.getInt("g_start");
            int g_e = rs.getInt("g_end");
 
            System.out.println(gno + "\t" + gname + "\t" + g_s + "\t" + g_e);
        } // while end
 
        // 4.자원반환(닫기)
        rs.close();
        stmt.close();
        conn.close();
    }
 
    
    // INSERT
    public static void insert() throws ClassNotFoundException, SQLException {
        Scanner sc = new Scanner(System.in);
        Connection conn = connection();
        System.out.println("<<<INSERT>>>");
 
        // 3.사용(DML 명령어 - insert)
        Statement stmt = conn.createStatement();
 
        System.out.print("상품번호를 입력해주세요=>");
        int gno = sc.nextInt();
        System.out.print("상품명을 입력해주세요=>");
        String gname = sc.next();
        System.out.print("최저가를 입력해주세요=>");
        int g_start = sc.nextInt();
        System.out.print("최고가를 입력해주세요=>");
        int g_end = sc.nextInt();
 
        String sql = "INSERT INTO GIFT VALUES(" + gno + ",'" + gname + "'," + g_start + "," + g_end + ")";
 
        int result = stmt.executeUpdate(sql);
        System.out.println(result + "개 데이터 추가 성공");
 
        // 4.자원반환(닫기)
        stmt.close();
        conn.close();
    }
 
    
    // UPDATE
    public static void update() throws ClassNotFoundException, SQLException {
        Scanner sc = new Scanner(System.in);
        Connection conn = connection();
        System.out.println("<<<UPDATE>>>");
 
        // 3.사용(DML 명령어 - update)
        Statement stmt = conn.createStatement();
 
        System.out.print("수정할 상품번호를 입력해주세요=>");
        int num = sc.nextInt();
 
        while (true) {
            System.out.print("수정할 필드명을 골라주세요(상품명:1, 최저가:2, 최고가:3)=>");
            int update_num = sc.nextInt();
 
            switch (update_num) {
            case 1:
                System.out.print("변경할 상품명을 입력해주세요=>");
                String gname = sc.next();
                String sql1 = "update gift set gname= '" + gname + "' where gno=" + num;
                stmt.executeUpdate(sql1);
                break;
            case 2:
                System.out.print("변경할 최저가를 입력해주세요=>");
                int g_start = sc.nextInt();
                String sql2 = "update gift set g_start= " + g_start + " where gno=" + num;
                stmt.executeUpdate(sql2);
                break;
            case 3:
                System.out.print("변경할 최고가를 입력해주세요=>");
                int g_end = sc.nextInt();
                String sql3 = "update gift set g_end= " + g_end + " where gno=" + num;
                stmt.executeUpdate(sql3);
                break;
            default:
                System.out.print("잘못 입력하셨습니다. 다시 입력해주세요.\n");
                continue;
            } // end switch
            System.out.println(num + "번 상품 수정완료되었습니다.");
            
            System.out.print("수정을 계속하시겠습니까?(y/n)=>");
            String repeat = sc.next();
            if (repeat.equalsIgnoreCase("y")) {
                continue;
            } else {
                // 4.자원반환(닫기)
                stmt.close();
                conn.close();
                break;
            } //if end
        }//end while
    }
 
    
    // DELETE
    public static void delete() throws ClassNotFoundException, SQLException {
        Scanner sc = new Scanner(System.in);
        Connection conn = connection();
        System.out.println("<<<DELETE>>>");
 
        System.out.print("삭제할 상품의 번호를 입력해주세요=>");
        int num = sc.nextInt();
 
        // 3.사용(DML 명령어 - delete)
        Statement stmt = conn.createStatement();
        String sql = "DELETE FROM GIFT WHERE GNO=" + num;
 
        stmt.executeUpdate(sql);
        System.out.println(num + "번 상품 삭제가 완료되었습니다.");
 
        // 4.자원반환(닫기)
        stmt.close();
        conn.close();
    }
}
 
cs

 

 

'JAVA > 20_jdbc' 카테고리의 다른 글

executeQuery / executeUpdate  (0) 2021.02.18

+ Recent posts