day10

์ƒ์€๐Ÿ‘ธยท2024๋…„ 1์›” 14์ผ
0

๋šœ๋ฒ…๋šœ๋ฒ… ์„ธ๋ฒˆ์งธ

๋ชฉ๋ก ๋ณด๊ธฐ
10/11
post-thumbnail

๐Ÿ“Œ์—ฐ์Šต๋ฌธ์ œ

A์—…์ฒด์—์„œ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋Š” TABLE_DATA_1 ์— ์ €์žฅํ•˜๊ณ 
B์—…์ฒด์—์„œ ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋Š” TABLE_DATA_2 ์— ์ €์žฅํ•œ๋‹ค.
--์ด๋•Œ, A์—…์ฒด B์—…์ฒด๋กœ ๋ถ€ํ„ฐ ๊ฐ๊ฐ ๋“ค์–ด์˜ค๊ธฐ ๋•Œ๋ฌธ์— (์ถ”ํ›„ C, D ์—…์ฒด๋“ฑ ํ™•์žฅ ๊ฐ€๋Šฅ) ์–ธ์ œ ๋“ค์–ด์™”๋Š”์ง€ ํ™•์ธ๋ชฉ์ ์œผ๋กœ
--๋ฐ์ดํ„ฐ ์ทจํ•ฉ์—ฌ๋ถ€๋ฅผ ์ฒดํฌํ•˜๋Š”TABLE_COLC ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.
--๊ฐ ๋‚ ์งœ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์™”์œผ๋ฉด 'Y' ๋กœ ์•„์ง ์•ˆ๋“ค์–ด์™”์œผ๋ฉด 'N'์œผ๋กœ ํ‘œ๊ธฐํ•œ๋‹ค.
--CHECK_DATA1 ๊ณผ CHECK_DATA2 ๋ฅผ ์ทจํ•ฉ ํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜์„ธ์š”.

TABLE_DATA_1

CREATE TABLE TABLE_DATA_1
(
no number(10),
create_date DATE
);

TABLE_DATA_2

CREATE TABLE TABLE_DATA_2
(
no number(10),
create_date DATE
);

TABLE_COLC

CREATE TABLE TABLE_COLC
(
std_date DATE,
CHECK_DATA1 VARCHAR2(6),
CHECK_DATA2 VARCHAR2(6)
);

๐Ÿ‘€ MERGE INTO ๋ฅผ ์ด์šฉํ•ด์„œ ๋‘ํ…Œ์ด๋ธ”์„ ํ•ฉ์น˜์ž!!!!!
TABLE_DATA_1 ๋ณ‘ํ•ฉ

MERGE INTO TABLE_COLC tc
USING TABLE_DATA_1 t1
ON (tc.std_date = t1.create_date)
WHEN MACTHED THEN --์กฐ๊ฑด๊ณผ ์ผ์น˜ํ•˜๋ฉด 
	UPDATE SET CHECK_DATA1 = 'Y'
WHEN NOT MACTHED THEN --์กฐ๊ฑด๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š์„๋•Œ
	INSERT VALUES (t1.create_date, 'Y', 'N');
	--TABLE_DATA_1์˜ create_date ๋‚˜์˜ค๊ณ  
    --์–˜๊ฐ€ ๋‚˜์˜ค๋Š”๊ฑฐ๋ฉด CHECK_DATA1์€ Y ์ธ๊ฑฐ๊ณ  CHECK_DATA2๋Š” N ์ธ๊ฑฐ๊ณ 

TABLE_DATA_2 ๋ณ‘ํ•ฉ

MERGE INTO TABLE_COLC tc
USING TABLE_DATA_2 t2
ON (tc.std_date = t2.create_date)
WHEN MACTHED THEN --์กฐ๊ฑด๊ณผ ์ผ์น˜ํ•˜๋ฉด 
	UPDATE SET CHECK_DATA1 = 'Y'
WHEN NOT MACTHED THEN --์กฐ๊ฑด๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š์„๋•Œ
	INSERT VALUES (t2.create_date, 'N', 'Y');
	--TABLE_DATA_2์˜ create_date ๋‚˜์˜ค๊ณ  
    --์–˜๊ฐ€ ๋‚˜์˜ค๋Š”๊ฑฐ๋ฉด CHECK_DATA2๋Š” Y ์ธ๊ฑฐ๊ณ  CHECK_DATA2๋Š” N ์ธ๊ฑฐ๊ณ 

๐Ÿ“– ์ž๋ฐ” <--> ์˜ค๋ผํดDB ์—ฐ๊ฒฐ

//DB ์—ฐ๊ฒฐ ๊ด€๋ จ๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋กœ๋”ฉ
try {
	Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
	e.printStackTrace();
}
		
//DB ์—ฐ๊ฒฐ์— ํ•„์š”ํ•œ ์ •๋ณด ๋ณ€์ˆ˜๋กœ ์„ ์–ธ (url, id, pw)
//๋‚ด ์ž์ฒด์˜ PC์˜ ์ฃผ์†Œ : localhost,127.0.0.1 ip์ฃผ์†Œ:ํฌํŠธ๋ฒˆํ˜ธ:orcl
String db_url = "jdbc:oracle:thin@localhost:1521:orcl";
String db_id = "scott";
String db_pw = "tiger";
		
		
//DB ์—ฐ๊ฒฐ Connection ๊ฐ์ฒด -> ๋‚˜์ค‘์— ์“ฐ๋ ค๊ณ  ์„ ์–ธ๋งŒ ํ•ด๋‘” ์ƒํƒœ / ์™ธ๋ถ€์—์„œ ์ ‘๊ทผํ• ์ˆ˜์žˆ๊ฒŒ!
Connection conn = null; //import
		
		
//์˜ˆ์™ธ์ฒ˜๋ฆฌ (์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊ฑฐ๋ƒ) : try catch
try {
	//์ฃผ์–ด์ง„ url, id, pw ๋ฅผ ๊ฐ€์ง€๊ณ  DB ์—ฐ๊ฒฐ ๊ฐ์ฒด๋ฅผ ํš๋“
	conn = DriverManager.getConnection(db_url, db_id, db_pw);
} catch (SQLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
		
		
//conn์„ ํ™œ์šฉํ•ด์„œ DB ๋ฐ์ดํ„ฐ ์ €์žฅ, ์กฐํšŒ, ์ˆ˜์ •, ์‚ญ์ œ
try {
	conn.close();
} catch (SQLException e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
} //DB Connection ์—ฐ๊ฒฐ ์ข…๋ฃŒ

๐Ÿ“– ์˜ˆ์™ธ ์ฒ˜๋ฆฌ

: ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋Œ€์ฒ˜

(1) ์ปดํŒŒ์ผ ์˜ˆ์™ธ / ์ผ๋ฐ˜์˜ˆ์™ธ / Checked Exception
=> ์ปดํŒŒ์ผ ๋‹จ๊ณ„์—์„œ ํ™•์ธ์ด ๊ฐ€๋Šฅํ•œ ์˜ค๋ฅ˜

(2) ๋Ÿฐํƒ€์ž„ ์˜ˆ์™ธ / ์‹คํ–‰์˜ˆ์™ธ / UnChecked Exception
=> ์‹คํ–‰ํ•ด์•ผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ์˜ค๋ฅ˜

๐Ÿ“Œtry ~ catch

try {
	์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ๋Š” ์ฝ”๋“œ
} catch (Exception e) {
	try์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ ๋Œ€์ฒ˜ ์ฝ”๋“œ
}

๐Ÿ“Œtry ~ catch ~ finally

try {
	A
} catch (Exception e) { //try์—์„œ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉด ์‹คํ–‰
	B
} finally { //๋ฌด์กฐ๊ฑด ์‹คํ–‰!
	C
}

try ์˜ค๋ฅ˜ ๋ฐœ์ƒO => A -> B -> C
try ์˜ค๋ฅ˜ ๋ฐœ์ƒX => A -> C

=> try ์— ์˜ค๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉด catch + finally
=> try ์— ์˜ค๋ฅ˜๊ฐ€ ์—†์œผ๋ฉด finally

try {
	System.out.println(arr[5]);
} catch (Exception e) {
	System.out.println("Catch 1");
} // Catch 1
		
try {
	System.out.println(arr[1]);
} catch (Exception e) {
	System.out.println("Catch 2");
} // 2
		
try {	
	System.out.println(arr[1]);
} catch (Exception e) {
	System.out.println("Catch 3");
} finally {
	System.out.println("Finally 3"); //๋ฌด์กฐ๊ฑด์‹คํ–‰
} // 2 Finally 3
		
try {
	System.out.println(arr[7]);
} catch (Exception e) {
	System.out.println("Catch 4");
} finally {
	System.out.println("Finally 4"); //๋ฌด์กฐ๊ฑด์‹คํ–‰
} // Catch 4 Finally 4

๐Ÿ“Œ

๋ฉ”์†Œ๋“œ() throws Exception ๋ช…์‹œ
-> ๋‚  ํ˜ธ์ถœํ•œ ์ชฝ์— ์ด ๋ฉ”์†Œ๋“œ๋Š” ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ• ์ˆ˜์žˆ์Œ์„ ์•Œ๋ฆฌ๋Š” ์—ญํ•  (๊ฒฝ๊ณ ) -> ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•œ ๋ฉ”์†Œ๋“œ์ž…๋‹ˆ๋‹ค!

throw new Exception()
-> ์ง์ ‘ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ ์‹œํ‚ค๋Š” ์—ญํ• !
=> ์œ„์ชฝ(๋‚  ํ˜ธ์ถœํ•œ ์ชฝ)์— ์˜ˆ์™ธ ๋ฐœ์ƒ์„ ์ „๋‹ฌ! (์ฑ…์ž„์ „๊ฐ€)

try {
	ex.addItem3("์„ธ๋ฒˆ์งธ"); //์˜ค๋ฅ˜X -> if์ ˆ ๊ฑธ๋ ค์žˆ์Œ
} catch (Exception e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}

void addItem3(String str) throws Exception { //Exception์„ ๋˜์ง„๋‹ค : ์˜ˆ์™ธ๋ฐœ์ƒํ• ์ˆ˜๋„์žˆ์œผ๋‹ˆ๊นŒ ์˜ˆ์™ธ์ฒ˜๋ฆฌํ•˜์„ธ์š”~
											//์ฑ…์ž„์ „๊ฐ€..ใ…‹ใ…‹ ๋งž๋Š”์ฝ”๋“œ์—ฌ๋„ ์ปดํŒŒ์ผ๋‹จ๊ณ„๋ถ€ํ„ฐ ๋นจ๊ฐ„์ค„์ด ๋œธ
		
if (list == null) { //null์ด๋ฉด ์•ˆ๋˜๋‹ˆ๊นŒ null ์ผ๋•Œ list ๋งŒ๋“ค์–ด์ค€๋‹ค ๋ผ๊ณ  ์กฐ๊ฑด์‹์œผ๋กœ ๊ฑธ์ˆ˜๋„!
	list = new ArrayList<String>();
	throw new Exception(); //์ผ๋ถ€๋Ÿฌ ์˜ˆ์™ธ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ -> ์œ„๋กœ ์˜ฌ๋ ค
}
		
list.add(str); //์˜ˆ์™ธ๋ฐœ์ƒ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋Š” ์ฝ”๋“œ}
profile
๋’ค์ฃฝ๋ฐ•์ฃฝ ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€