개발 수업/DB
[Java,DB] DML한 클래스에서 실행, 시퀀스
오늘 하루s
2023. 6. 5. 19:45
728x90
더보기
Day26-1. 230814
DML한 클래스에서 실행
DML을 한 클래스에서 실행 할 수 있도록 작업을 하였다.
1. JDBC Driver등록
2. 연결 Connection얻기
3. 객체준비
4. 쿼리실행
5. 자원반납
과정에서 1,2 실행하는 메소드를 따로 빼놓고
입력,수정,삭제,조회는 각각 객체준비, 쿼리실행, 자원반납을 해주었다.
그리고 get메소드를 사용해 각각을 불러주어 한 클래스 안에서 DML을 진행했다.
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
|
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/*라이브러리 등록 - 해당프로젝트 Build Path -> add External Library -> ojdbc6.jar
1. JDBC Driver등록
2. 연결 Connection얻기
3. 객체준비
4. 쿼리실행
5. 자원반납
*/
public class DMLEx {
//field
String url ="jdbc:oracle:thin:@172.30.1.86:1521/xe";
String user = "scott";
String password = "tiger";
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
//construct
//method
//1. JDBC Driver등록 &2. 연결 Connection얻기
public void getDBConnction() {
//1. JDBC Driver등록
try {
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("1.JDBC Driver등록");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2. 연결 Connection얻기
try {
conn = DriverManager.getConnection(url, user, password);
System.out.println("2.연결 Connection얻기-성공");
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//1. JDBC Driver등록 &2. 연결 Connection얻기
DMLEx obj = new DMLEx();
obj.getDBConnction();
//3. 객체준비&4. 쿼리실행
//목록조회->입력->목록조회->수정->상세조회->삭제
System.out.println();
System.out.println("--목록조회결과--");
obj.getDeptList();
System.out.println();
System.out.println("--부서정보입력--");
obj.insertDept("개발부97","우도");
System.out.println();
System.out.println("--목록조회결과--");
obj.getDeptList();
//수정-97번의 부서명은 개발부, 울릉도
System.out.println();
System.out.println("--수정하기--");
//obj.updateDept("울릉도","개발부",97);
System.out.println();
System.out.println("--상세조회결과--");
//obj.getDept(10);
//삭제
System.out.println();
System.out.println("--삭제하기--");
obj.deleteDept(97);
//obj.getDeptList();
//5. 자원반납
//Connection자원반납
obj.connClose();
}//main
//삭제하기
public void deleteDept(int dno) {
String sql = " delete from dept " +
"where deptno=?";
//3.객체준비
try {
stmt = conn.prepareStatement(sql);
} catch (SQLException e1) {
e1.printStackTrace();
}
//4.쿼리실행
try {
stmt.setInt(1,dno);
int cnt = stmt.executeUpdate();
if(cnt>0) {
System.out.println("쿼리실행성공="+cnt);
}
} catch (SQLException e1) {
System.out.println("executeUpdate()실행관련 에러발생");
e1.printStackTrace();
}
//5.자원반납
try {
if(stmt!=null) {stmt.close();}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("5.자원반납");
}
//수정하기
public void updateDept(String location,String name, int dno) {
String sql = "update dept " +
"set loc=?,dname=? " +
" where deptno=?";
//3.객체준비
try {
stmt = conn.prepareStatement(sql);
} catch (SQLException e1) {
e1.printStackTrace();
}
//4.쿼리실행
try {
stmt.setString(1,location);
stmt.setString(2,name);
stmt.setInt(3,dno);
int cnt = stmt.executeUpdate();
if(cnt>0) {
System.out.println("쿼리실행성공="+cnt);
}
} catch (SQLException e1) {
System.out.println("executeUpdate()실행관련 에러발생");
e1.printStackTrace();
}
//5.자원반납
try {
if(stmt!=null) {stmt.close();}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("5.자원반납");
}
//입력하기
public void insertDept(String dname, String loc) {
String sql = "insert into dept(deptno,dname,loc) "
+ "values(seq_dno.NEXTVAL,?,?)";
//3.객체준비
try {
stmt = conn.prepareStatement(sql);
} catch (SQLException e1) {
System.out.println();
e1.printStackTrace();
}
//4.쿼리실행
try {
stmt.setString(1,dname);
stmt.setString(2,loc);
int cnt = stmt.executeUpdate();
if(cnt>0) {
System.out.println("쿼리실행성공="+cnt);
}
} catch (SQLException e1) {
System.out.println("executeUpdate()실행관련 에러발생");
e1.printStackTrace();
}
//5.자원반납
try {
if(stmt!=null) {stmt.close();}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("5.자원반납");
}
//부서번호에 따른 상세조회
public void getDept(int dno) {
String sql = "select deptno as dno, dname as name, loc as location "
+"from dept "
+"where deptno=?";
//3.객체준비
try {
stmt = conn.prepareStatement(sql);
} catch (SQLException e1) {
System.out.println("객체관련 에러발생="+e1);
e1.printStackTrace();
}
//4.쿼리실행
try {
stmt.setInt(1, dno);
rs = stmt.executeQuery();
System.out.println("부서번호\t부서명\t\t위치");
while(rs.next()) {
int d_no = rs.getInt("dno"); //첫번째 컬럼
String d_name = rs.getString("name");
String loc = rs.getString("location");
System.out.printf("%5d\t %14s\t %13s\r\n", d_no,d_name,loc);
}
} catch (SQLException e1) {
System.out.println("executeUpdate()실행관련 에러발생");
e1.printStackTrace();
}
//5. 자원반납
try {
if(rs!=null) {rs.close();}
if(stmt!=null) {stmt.close();}
} catch (SQLException e) {
e.printStackTrace();
}
}
//목록조회
public void getDeptList() {
String sql = "select deptno as dno, dname as name, loc as location "
+"from dept";
//3.객체준비
try {
stmt = conn.prepareStatement(sql);
} catch (SQLException e1) {
System.out.println("객체관련 에러발생="+e1);
e1.printStackTrace();
}
//4.쿼리실행
try {
rs = stmt.executeQuery();
System.out.println("부서번호\t부서명\t\t위치");
while(rs.next()) {
int d_no = rs.getInt("dno"); //첫번째 컬럼
String d_name = rs.getString("name");
String loc = rs.getString("location");
System.out.printf("%5d\t %14s\t %13s\r\n", d_no,d_name,loc);
}
} catch (SQLException e1) {
System.out.println("executeUpdate()실행관련 에러발생");
e1.printStackTrace();
}
//5. 자원반납
try {
if(rs!=null) {rs.close();}
if(stmt!=null) {stmt.close();}
} catch (SQLException e) {
e.printStackTrace();
}
}
//5.Connection 자원반납
public void connClose() {
try {
if(conn!=null) {conn.close();}
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("Connection자원반납");
}
}
|
cs |
DMLEx{
//field
//constuct
//method
1. JDBC Driver등록 &2. 연결 Connection얻기
public static void main(String[] args) {
1. JDBC Driver등록 &2. 연결 Connection얻기
3. 객체준비&4. 쿼리실행 목록조회->입력->목록조회->수정->상세조회->삭제
5. 자원반납
}//main
//삭제하기
//수정하기
//입력하기
//부서번호에 따른 상세조회
//목록조회
//5.Connection 자원반납
}
시퀀스(Sequence)
: 자동 순번을 반환하는 데이터베이스 객체.
단순한 증감 연산을 이용해 유일한 숫자를 구할 때 시퀀스를 사용하면 매우 편리
시퀀스 생성
create sequence 시퀀스명
[increment by 증감]
[start with 시작번호]
[minvalue 최솟값]
[maxvalue 최댓값]
[nocycle | cycle]
[nocache | cache];
시퀀스명 seq_dno
1씩 증감, 61번 부터 시작, nocycle
시퀀스 수정
alter sequence 시퀀스명
[increment by 증감]
[minvalue 최솟값]
[maxvalue 최댓값]
[nocycle | cycle]
[nocache | cache];
시퀀스 삭제
drop sequence 시퀀스명;
시퀀스 조회
select *
from user_sequences;
시퀀스 사용
시퀀스명.NEXTVAL
=>예문) insert into 테이블명 values(시퀀스명.NEXTVAL);
해당 시퀀스에서 다음 순번 값을 자동으로 가져옴.
예문) insert into dept(dname) values(seq_dno.NEXTVAL);
61번 부터 시작했을 때
61번 생성
62번 생성
시퀀스명.CURRVAL
=>예문) select 시퀀스명.CURRVAL from dual;
해당 시퀀스의 현재 값을 알 수 있음.
예문) select seq_dno.CURRVAL from dual;
ex)
alther sequence seq_dno
increment by 1
maxvalue 99
nocycle;
insert into dept(deptno, dname, loc)
values(seq_dno.NEXTVAL,'dept','jeju');
728x90