[Day2] JSP/JDBC 2 [10/27]

 

--๋ณต์Šต๋ฌธ์ œ--

1. ๋™์  ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ € ์ž‘์„ฑํ•˜์„ธ์š”.  ( 5๋ถ„ )

ใ„ฑ. ์ปค์„œ๋ณ€์ˆ˜ ์‚ฌ์šฉ

ใ„ด. open ~ for ๋ฌธ ์‚ฌ์šฉ

ใ„ท. ํ”„๋กœ์‹œ์ € ์•ˆ์—์„œ  ์ถœ๋ ฅ๊นŒ์ง€ ํ•˜์„ธ์š”.

ใ„น. deptno ๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ํ•ด์„œ emp ํ…Œ์ด๋ธ”์˜ deptno, ename, hiredate ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ €์žฅ ํ”„๋กœ์‹œ์ € ์ž‘์„ฑ.

 

CREATE OR REPLACE PROCEDURE up_emp
(
  pdeptno emp.deptno%TYPE
)
IS
vsql varchar2(2000);
vempCursor SYS_REFCURSOR;
vdeptno emp.deptno%TYPE;
vename emp.ename%TYPE;
vhiredate emp.hiredate%TYPE;
BEGIN
vsql := 'SELECT deptno, ename, hiredate ';
vsql := vsql || 'FROM emp ';
vsql := vsql || 'WHERE deptno = :deptno ';

OPEN vempCursor FOR vsql USING pdeptno;
  LOOP
    FETCH vempCursor INTO vdeptno, vename, vhiredate;
      EXIT WHEN vempCursor%NOTFOUND;
        DBMS_OUTPUT.PUT_LINE( vdeptno || ', ' || vename || ', ' || vhiredate );
  END LOOP;
CLOSE vempCursor;
--EXCEPTION
END;

EXEC up_emp(20);

 

2-1. JDBC ์„ค๋ช…ํ•˜์„ธ์š”.               

- J D B C

- ์ž๋ฐ” ํ‘œ์ค€ ์ธํ„ฐํŽ˜์ด์Šค  : ์ถ”์ƒ๋ฉ”์„œ๋“œ, ์ƒ์ˆ˜ + s๋ฉ”, d๋ฉ”

- ์ž๋ฐ” + DB ์—ฐ๋™ ๊ธฐ์ˆ 

 

2-2. JDBC Driver ์„ค๋ช…ํ•˜์„ธ์š”.

- DBMS(์˜ค/M)    ์—ฐ๋™ํ•˜๊ธฐ ์œ„ํ•ด   JDBC๋ฅผ ๊ตฌํ˜„ํ•œ   ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(๋“œ๋ผ์ด๋ฒ„)

- Oralce :  JDBC Driver ๋ฒค๋”(ํšŒ์‚ฌ) ๋‹ค์šด  ojdbc6.jar  == > Oracle 11g xe

     

2-3. JDBC Driver [์ข…๋ฅ˜] ์„ค๋ช…ํ•˜์„ธ์š”.

- Type1     ODBC ๋ธŒ๋ฆฟ์ง€ X

- Type2    Native API    C/C++ X

- Type3   ๋ฏธ๋“ค์›จ์–ด ์„œ๋ฒ„ X

- Type4  ์ˆœ์ˆ˜ ์ž๋ฐ” , ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.   thin

 

3. JDBC๋ฅผ ์‚ฌ์šฉํ•ด์„œ DB์— ์—ฐ๊ฒฐ( Connect ) ํ•˜๋Š” ์ˆœ์„œ์— ๋Œ€ํ•ด ์„ค๋ช…ํ•˜์„ธ์š”.  

1) JDBC ๋“œ๋ผ์ด๋ฒ„ ๋กœ๋”ฉ           :   Class.forName()

2) Connecion ๊ฐ์ฒด ์–ป์–ด์˜ค๊ธฐ :   DriverManager.getConnection ()

3) ํ•„์š”ํ•œ ์กฐ์ž‘ ( CRUD )

4) Close

 

4. ์œ„์˜ ์ˆœ์„œ์— ๋งž์ถฐ์„œ ์˜ค๋ผํด์„œ๋ฒ„์— scott/tiger ๋กœ DB์— ์—ฐ๊ฒฐํ•˜๋Š” ์ „์ฒด ์ฝ”๋”ฉ์„ main() ๋ฉ”์„œ๋“œ์— ํ•˜์‹œ๊ณ , ์—ฐ๊ฒฐ๋œ Connection ๊ฐ์ฒด ์ •๋ณด๋ฅผ  ์ถœ๋ ฅํ•˜๊ณ   Connection ๊ฐ์ฒด ์—ฐ๊ฒฐ ์ƒํƒœ๋ฅผ true, false๋กœ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋”ฉ์„ ํ•˜์„ธ์š”. 

import java.sql.Connection;  
main(){

Connection conn = null;

String className = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:xe";  // SID   xe
String user = "scott";
String password = "tiger";

Class.forName(className);
conn = DriverManager.getConnection (url, user, password);
Syso( conn );
Syso( conn.is์ƒํƒœ );

conn.close();

ClassNotFoundException
SQLException
}

 

5. ๋ถ€์„œ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ํ•ด๋‹น ๋ถ€์„œ์˜ ๋ชจ๋“  ์‚ฌ์› ์ •๋ณด๋ฅผ ์ถœ๋ ฅ 

( ์กฐ๊ฑด :

  ใ„ฑ. ๋ชจ๋“  ์ปฌ๋Ÿผ ์ถœ๋ ฅ

  ใ„ด. ์ด๋ฆ„ ์ˆœ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์ •๋ ฌ 

  ใ„ท. domain.EmpDTO ์„ ์–ธ

  ใ„น. ArrayList ์‚ฌ์šฉํ•ด์„œ  EmpDTO ๊ฐ์ฒด ์ €์žฅ

  ใ…. printEmp() ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•ด์„œ ์‚ฌ์› ์ •๋ณด ์ถœ๋ ฅ

  ใ…‚. ์‚ฌ์›์ด ์—†์„ ๊ฒฝ์šฐ "์‚ฌ์›์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."๋ผ๊ณ  ์ถœ๋ ฅ. 

)     

 

public static void main(String[] args) {
	Scanner scanner = new Scanner(System.in);
	System.out.print("> ๋ถ€์„œ๋ฒˆํ˜ธ ์ž…๋ ฅ ? ");		
	int pdeptno = scanner.nextInt();  // 10~40
		
	String sql = "SELECT * "
				          + " FROM emp "
				          + " WHERE deptno = " + pdeptno;
	// 1 + 2.
	Connection conn =  DBConn.getConnection();

	// 3.
	Statement stmt = null;
	ResultSet rs = null;
    
    // ์‚ฌ์›์ด ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉด empList ๊ฐ์ฒด ์ƒ์„ฑ X
	ArrayList<EmpDTO> empList = null;

	int empno;
	String ename;
	String job;
	Date hiredate;
	int mgr;
	double sal;
    double comm;
    int deptno;
		
    try {
		stmt =  conn.createStatement();
			
		rs = stmt.executeQuery(sql);  // select
        // stmt.executeUpdate(sql);  insert, update, delete

		if( rs.next()) {
			empList = new ArrayList<EmpDTO>();
			do {
				empno = rs.getInt("empno");
				ename = rs.getString("ename");
				job = rs.getString("job");
				hiredate = rs.getDate("hiredate");
				mgr = rs.getInt("mgr");
				sal = rs.getDouble("sal");
				comm = rs.getDouble("comm");
                deptno = rs.getInt("deptno");
					
				EmpDTO dto = new EmpDTO(empno, ename, job, hiredate, mgr, sal, comm, deptno);
				
				empList.add(dto);
			}while( rs.next() );
		} 
		printEmp( empList )	;	
			

	} catch (SQLException e) { 
		e.printStackTrace();
	} finally {			
		try {
			rs.close();
			stmt.close();				
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 4.
		DBConn.close();
	} 	
	System.out.println( "END");
} // main


private static void printEmp(ArrayList<EmpDTO> empList) {
	try {
		Iterator<EmpDTO>  ir = empList.iterator();		
		//  java.lang.[NullPointerException]
		while (ir.hasNext()) {
			EmpDTO dto =  ir.next();
			System.out.println( dto );
		} // while
	} catch (NullPointerException e) { 
		  System.out.println("์‚ฌ์›์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
	}catch (Exception e) {
		 e.printStackTrace();
	}	
} // printEmp

 


// ์ธ์Šคํ„ด์Šค ๋ณ€์ˆ˜
// ํด๋ž˜์Šค ๋ณ€์ˆ˜ == ์ •์  ๋ณ€์ˆ˜ == static ๋ณ€์ˆ˜ == ๊ณต์œ  ๋ณ€์ˆ˜ 
private static int selectedNumber;
private static Scanner scanner = new Scanner(System.in);
private static Connection conn ;
private static Statement stmt;
private static ResultSet  rs ;
private static String [] menus = {
	"๋ถ€์„œ ์กฐํšŒ"
	, "๋ถ€์„œ ์ถ”๊ฐ€"
	, "๋ถ€์„œ ์ˆ˜์ •"
	, "๋ถ€์„œ ์‚ญ์ œ"
	, "๋ถ€์„œ ๊ฒ€์ƒ‰"
	, "์ข…๋ฃŒ"
};
public static void main(String[] args) {
	// java.lang.NullPointerException
	// EmpDTO dto = null;
	// dto.setEname("ํ™๊ธธ๋™");		
	conn = DBConn.getConnection();
	
	while( true ) {
		๋ฉ”๋‰ด์ถœ๋ ฅ();
		๋ฉ”๋‰ด์„ ํƒ();
		๋ฉ”๋‰ด์ฒ˜๋ฆฌ();
	} // while
} // main

1) ๋ฉ”๋‰ด์ถœ๋ ฅ()

private static void ๋ฉ”๋‰ด์ถœ๋ ฅ() {
		System.out.println("> ๋ฉ”๋‰ด ์ถœ๋ ฅ <");
		for (int i = 0; i < menus.length; i++) {
		System.out.printf("%d. %s\n", ( i+1),   menus[i] );
	} // for
}

2) ๋ฉ”๋‰ด์„ ํƒ()

 private static void ๋ฉ”๋‰ด์„ ํƒ() {
	System.out.print("> ๋ฉ”๋‰ด ์„ ํƒํ•˜์„ธ์š” ? ");
	selectedNumber = scanner.nextInt();
}

3) ๋ฉ”๋‰ด์ฒ˜๋ฆฌ()

private static void ๋ฉ”๋‰ด์ฒ˜๋ฆฌ() {
	switch (  selectedNumber  ) {
	case 1:  // ๋ถ€์„œ ์กฐํšŒ	-  dept ํ…Œ์ด๋ธ”์˜ ๋ชจ๋“  ๋ถ€์„œ์ •๋ณด ์ถœ๋ ฅ. 		
		selectAllDept();
		break;
	case 2:  // ๋ถ€์„œ ์ถ”๊ฐ€			
		insertDept();
		break;
	case 3:  // ๋ถ€์„œ ์ˆ˜์ •	
		updateDept();
		break;			
	case 4:  // ๋ถ€์„œ ์‚ญ์ œ		
		deleteDept();
		break;			
	case 5:  // ๋ถ€์„œ ๊ฒ€์ƒ‰			
		searchDept();
		break;	
	case 6:  // ์ข…๋ฃŒ		
		exit();
		break;	
	default:
		System.out.println("\t\t[๊ฒฝ๊ณ ] ๋ฉ”๋‰ด ์„ ํƒ ์ž˜๋ชปํ–ˆ๋‹ค( 1~6)!!!");
		break;
	} // switch
	์ผ์‹œ์ •์ง€();
} // ๋ฉ”๋‰ด์ฒ˜๋ฆฌ

3-1) ๋ถ€์„œ ์กฐํšŒ

private static void selectAllDept() {
	String sql = "SELECT * "
			+ " FROM dept"
			+ " ORDER BY deptno  ASC";
	
	ArrayList<DeptDTO> deptList = null;
	
	try {
		stmt = conn.createStatement();
		rs = stmt.executeQuery(sql);
		
		if( rs.next() ) {
			deptList =  new ArrayList<DeptDTO>();
			do {					
				DeptDTO dto = new DeptDTO(  rs.getInt("deptno") , rs.getString("dname"),  rs.getString("loc"));					
				deptList.add(dto);					
			} while (rs.next());
		} // if
		
		printDept( deptList );
	} catch (SQLException e) { 
		e.printStackTrace();
	} finally {
		try {
			stmt.close();
			rs.close();
			// DBConn.close();  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒํ•  ๋•Œ ๊ทธ ๋•Œ ํ•œ ๋ฒˆ๋งŒ ๋‹ซ๊ธฐ.
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	} // try
}

3-2) ๋ถ€์„œ ์ถ”๊ฐ€

private static char _continue;  //  'y'  'n'
private static void insertDept() {
	do {
		// 1. ๋ถ€์„œ๋ฒˆํ˜ธ X , [๋ถ€์„œ๋ช…], [์ง€์—ญ๋ช…] ์ž…๋ ฅ
		//    seq_dept ์‹œํ€€์Šค  50, 10
		System.out.println("[๋ถ€์„œ ์ •๋ณด ์ž…๋ ฅ]");
		System.out.print("1. ๋ถ€์„œ๋ช… ์ž…๋ ฅ ? ");
		String pdname = scanner.next();
		System.out.print("2. ์ง€์—ญ๋ช… ์ž…๋ ฅ ? ");
		String ploc = scanner.next();
		
		// String sql = "INSERT INTO dept ( deptno, dname, loc ) VALUES ( seq_dept.nextval, '"+pdname+"', '"+ploc+"' )";
		String sql = String.format("INSERT INTO dept ( deptno, dname, loc ) VALUES ( seq_dept.nextval, '%s', '%s' )" , pdname, ploc);
			
		try {
			// ์ปค๋ฐ‹์„ ํ•˜์ง€ ์•Š์•„๋„ ์ž๋™ ์ปค๋ฐ‹๋˜๋„๋ก ์„ค์ •์ด ๋˜์–ด์ ธ ์žˆ๋‹ค.
			// conn.setAutoCommit(true);
		
			stmt =  conn.createStatement();
			// rs = stmt.executeQuery(sql);  // SELECT
			int rowCount = stmt.executeUpdate(sql);    // INSERT, UPDATE, DELETE
			if( rowCount == 1) {
				System.out.println("> 1๊ฐœ ๋ถ€์„œ ์ถ”๊ฐ€ ์™„๋ฃŒ!!!");
			}
		} catch (SQLException e) { 
			e.printStackTrace();
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} // try
	
		/*
		DROP SEQUENCE seq_dept;
		--
		CREATE SEQUENCE seq_dept
		INCREMENT BY 10
		START WITH 50
		MAXVALUE 90
		NOCYCLE
		NOCACHE;
		*/

		๊ณ„์†์—ฌ๋ถ€ํ™•์ธ();
	} while (    Character.toUpperCase(_continue)  == 'Y'  );
}
private static void ๊ณ„์†์—ฌ๋ถ€ํ™•์ธ() {
	System.out.print("> ๊ณ„์† ํ• ๊ฑฐ๋ƒ ? ");
	try {
		_continue =  (char)System.in.read();
		System.in.skip( System.in.available()  ); // 13, 10 ์ œ๊ฑฐ
} catch (IOException e) { 
		e.printStackTrace();
	}
}

3-3) ๋ถ€์„œ ์ˆ˜์ •

// ๋ถ€์„œ ์ˆ˜์ •
private static void updateDept() {
	System.out.print("> ์ˆ˜์ •ํ•  ๋ถ€์„œ๋ฒˆํ˜ธ(deptno) ์ž…๋ ฅ ? ");
	int pdeptno = scanner.nextInt();
	System.out.print("> ์ˆ˜์ •ํ•  ๋ถ€์„œ๋ช…, ์ง€์—ญ๋ช… ์ž…๋ ฅ ? ");
	String pdname = scanner.next();
	String ploc = scanner.next();
		
		
	String sql = String.format( 
	               " UPDATE dept "
	            + " SET dname = '%s' , loc = '%s' "
	            + " WHERE deptno = %d "
	            ,  pdname, ploc, pdeptno );
			
	// 1.  2.   
		
	// 3. UPDATE ์ฒ˜๋ฆฌ
	try {
		stmt = conn.createStatement();
	    int rowCount =  stmt.executeUpdate(sql); // UPDATE
	    if( rowCount == 1 ) {
	    	System.out.println("> 1๊ฐœ ๋ถ€์„œ ์ˆ˜์ • ์™„๋ฃŒ!!!");
	    }
	} catch (SQLException e) {
		 
		e.printStackTrace();
	} finally {
		try {
			stmt.close();
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	}
}

3-4) ๋ถ€์„œ ์‚ญ์ œ

// ๋ถ€์„œ ์‚ญ์ œ
private static void deleteDept() {
	System.out.print("> ์‚ญ์ œํ•  ๋ถ€์„œ๋ฒˆํ˜ธ(deptno) ์ž…๋ ฅ ? ");
	int pdeptno = scanner.nextInt();		
	String sql = String.format( 
			               " DELETE FROM  dept " 
			            + " WHERE deptno = %d "
			            , pdeptno );		 
	try {
		stmt = conn.createStatement();
	    int rowCount =  stmt.executeUpdate(sql);  
	    if( rowCount == 1 ) {
	    	System.out.println("> 1๊ฐœ ๋ถ€์„œ ์‚ญ์ œ ์™„๋ฃŒ!!!");
	    }
	} catch (SQLException e) {			 
		e.printStackTrace();
	} finally {
		try {
			stmt.close();
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	}	
}

3-5) ๋ถ€์„œ ๊ฒ€์ƒ‰

[๊ธฐ๋ณธ]

// ๋ถ€์„œ ๊ฒ€์ƒ‰
private static void searchDept() {
	System.out.print("> ๊ฒ€์ƒ‰ ์กฐ๊ฑด ์ž…๋ ฅ ? ");  // "dname"   "loc"
	String searchCondition = scanner.next();
	
	System.out.print("> ๊ฒ€์ƒ‰์–ด ์ž…๋ ฅ ? ");   
	String searchWord = scanner.next();
	
	// java.util.UnknownFormat[Conversion]Exception: Conversion = '''
	//    %s  %d  %c %f
	// ์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•
	/*
	String sql = String.format(
			"SELECT * "
			+ " FROM dept "
			+ " WHERE %s LIKE '%%%s%%'"
			, searchCondition, searchWord);
	*/
	 
	String sql = String.format(
			"SELECT * "
			+ " FROM dept "
			+ " WHERE %s LIKE '%s'  "
			, searchCondition, "%"+searchWord.toUpperCase()+"%");
	
	
	// selectAllDept ๋ฉ”์„œ๋“œ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ
    ArrayList<DeptDTO> deptList = null;
	
	try {
		stmt = conn.createStatement();
		rs = stmt.executeQuery(sql);
			
		if( rs.next() ) {
			deptList =  new ArrayList<DeptDTO>();
			do {					
				DeptDTO dto = new DeptDTO(  rs.getInt("deptno") , rs.getString("dname"),  rs.getString("loc"));					
				deptList.add(dto);					
			} while (rs.next());
		} // if
		
		printDept( deptList );
	} catch (SQLException e) { 
		e.printStackTrace();
	} finally {
		try {
			stmt.close();
			rs.close();
			// DBConn.close();  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒํ•  ๋•Œ ๊ทธ ๋•Œ ํ•œ ๋ฒˆ๋งŒ ๋‹ซ๊ธฐ.
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	} // try
}
private static void printDept(ArrayList<DeptDTO> deptList) {
	System.out.println("---------------------------");
	System.out.printf("%s\t%s\t%s\n", "DEPTNO", "DNAME", "LOC");
	System.out.println("---------------------------");
	
	try {
		Iterator<DeptDTO> ir = deptList.iterator();
		while (ir.hasNext()) {
			DeptDTO dto =  ir.next();
			System.out.println( dto );
		} // while
	} catch (NullPointerException e) {
		System.out.println(" ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ");
	} catch (Exception e) {
		// TODO: handle exception
	}
}

[๋™์ ์ฟผ๋ฆฌ]

private static void searchDept() {
	// [ PL/SQL ๋™์  ์ฟผ๋ฆฌ ]  OPEN ~ FOR ๋ฌธ
	 
	System.out.println("[ ๊ฒ€์ƒ‰ ์กฐ๊ฑด ]   "); 
	System.out.println("1) ๋ถ€์„œ๋ช…  ");
	System.out.println("2) ์ง€์—ญ๋ช…  ");
	System.out.println("3) ๋ถ€์„œ๋ช… + ์ง€์—ญ๋ช…  ");
	
	System.out.print("> ๊ฒ€์ƒ‰์กฐ๊ฑด ์„ ํƒ ? ");     // 1,  2, 3
	int searchCondition = scanner.nextInt();
	
	System.out.print("> ๊ฒ€์ƒ‰์–ด ์ž…๋ ฅ ? ");   
	String searchWord = scanner.next();
	
	String sql = " SELECT * "
			         + " FROM dept ";
		
	switch ( searchCondition) {
	case 1: 
		// PL/SQL์—์„œ๋Š” ์ปฌ๋Ÿฌ๋ช…, ํ…Œ์ด๋ธ”๋ช…์„ ๋ณ€์ˆ˜ ์‚ฌ์šฉ X ( ์•”๊ธฐ )
		sql += String.format(" WHERE dname LIKE '%s' ", "%"+ searchWord + "%");			
		break;
	case 2:
		sql += String.format(" WHERE  loc LIKE '%%%s%%' ",   searchWord );
		break;
	case 3: 
		sql += String.format(" WHERE dname LIKE '%1$s'  OR loc LIKE '%1$s'", "%"+ searchWord + "%");	
		break; 
	}
	
	sql += " ORDER BY deptno ASC";
	 
	// selectAllDept ๋ฉ”์„œ๋“œ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ
    ArrayList<DeptDTO> deptList = null;
	
	try {
		stmt = conn.createStatement();
		rs = stmt.executeQuery(sql);
		
		if( rs.next() ) {
			deptList =  new ArrayList<DeptDTO>();
			do {					
				DeptDTO dto = new DeptDTO(  rs.getInt("deptno") , rs.getString("dname"),  rs.getString("loc"));					
				deptList.add(dto);					
			} while (rs.next());
		} // if
		
		printDept( deptList );
	} catch (SQLException e) { 
		e.printStackTrace();
	} finally {
		try {
			stmt.close();
			rs.close();
			// DBConn.close();  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒํ•  ๋•Œ ๊ทธ ๋•Œ ํ•œ ๋ฒˆ๋งŒ ๋‹ซ๊ธฐ.
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	} // try
}

[REGEXP_LIKE]

private static void searchDept() {
	System.out.println("[ ๊ฒ€์ƒ‰ ์กฐ๊ฑด ]   "); 
	System.out.println("1) ๋ถ€์„œ๋ช…  ");
	System.out.println("2) ์ง€์—ญ๋ช…  ");
	System.out.println("3) ๋ถ€์„œ๋ช… + ์ง€์—ญ๋ช…  ");
	 
	System.out.print("> ๊ฒ€์ƒ‰์กฐ๊ฑด ์„ ํƒ ? ");     // 1,  2, 3
	int searchCondition = scanner.nextInt();
	
	System.out.print("> ๊ฒ€์ƒ‰์–ด ์ž…๋ ฅ ? ");   
	String searchWord = scanner.next();
	
	String sql = " SELECT * "
			         + " FROM dept ";
		
	switch ( searchCondition) {
	case 1: 
		// REGEXP_LIKE() ํ•จ์ˆ˜๋กœ ์ˆ˜์ •
		sql += String.format(" WHERE REGEXP_LIKE( dname, '%s', 'i' ) ", searchWord );			
		break;
	case 2:
		sql += String.format(" WHERE REGEXP_LIKE( loc, '%s', 'i' ) ", searchWord );			
		break;
	case 3: 
		sql += String.format(" WHERE REGEXP_LIKE( dname, '%1$s', 'i' ) OR  REGEXP_LIKE( loc, '%1$s', 'i' ) "
				, searchWord );			
		break; 
	}
	
	sql += " ORDER BY deptno ASC";
	 
	// selectAllDept ๋ฉ”์„œ๋“œ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ
    ArrayList<DeptDTO> deptList = null;
		
	try {
		stmt = conn.createStatement();
		rs = stmt.executeQuery(sql);
		
		if( rs.next() ) {
			deptList =  new ArrayList<DeptDTO>();
			do {					
				DeptDTO dto = new DeptDTO(  rs.getInt("deptno") , rs.getString("dname"),  rs.getString("loc"));					
				deptList.add(dto);					
			} while (rs.next());
		} // if
		
		printDept( deptList );
	} catch (SQLException e) { 
		e.printStackTrace();
	} finally {
		try {
			stmt.close();
			rs.close();
			// DBConn.close();  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒํ•  ๋•Œ ๊ทธ ๋•Œ ํ•œ ๋ฒˆ๋งŒ ๋‹ซ๊ธฐ.
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	} // try
}

3-6) ์ข…๋ฃŒ

private static void exit() {
	System.out.println("\t\t ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ !!!");
	DBConn.close();
	System.exit(-1);
}

 

3-7) ์ผ์‹œ์ •์ง€

private static void ์ผ์‹œ์ •์ง€() {
	System.out.println("\t\t ์—”ํ„ฐ์น˜๋ฉด ๊ณ„์†ํ•ฉ๋‹ˆ๋‹ค.");
	try {
		System.in.read();
 		System.in.skip( System.in.available() ); // 13, 10
	} catch (IOException e) { 
		e.printStackTrace();
	} // try
}

[์˜ค๋ผํด] ์ฟผ๋ฆฌ ์‹คํ–‰ ์ˆœ์„œ

 

1. ๊ตฌ๋ฌธ๋ถ„์„(Parsing)

- ์ž‘์„ฑํ•œ ์ฟผ๋ฆฌ๊ฐ€ ๋ฌธ๋ฒ•์ ์œผ๋กœ ํ‹€๋ฆฌ์ง€ ์•Š์€์ง€ ํ™•์ธํ•˜๋Š” ๋‹จ๊ณ„. ํ•ด๋‹น ์ฟผ๋ฆฌ๋ฅผ SQL ์„œ๋ฒ„๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์œ„๋กœ ๋ถ„ํ•ดํ•˜๋Š” ๊ณผ์ •์ด๋‹ค.

- ๋งŒ์•ฝ ๊ตฌ๋ฌธ์ด ๋ถ€์ •ํ™•(๋ฌธ๋ฒ•์ด ํ‹€๋ฆฌ๋‹ค๋ฉด)ํ•˜๋‹ค๋ฉด ์ด ๋‹จ๊ณ„์—์„œ ์ฒ˜๋ฆฌ๋ฅผ ์ค‘๋‹จํ•œ๋‹ค. ๋˜ํ•œ, ์ด ๋ฌธ์žฅ์ด ์ผ๊ด„์ฒ˜๋ฆฌ(batch)์— ์†ํ•ด ์žˆ๋‹ค๋ฉด ์ผ๊ด„ ์ฒ˜๋ฆฌ ์ „์ฒด๋ฅผ ์ค‘๋‹จํ•œ๋‹ค. 

 

2. ์ตœ์ ํ™”(Optimination)

- ์ฟผ๋ฆฌ ์ฒ˜๋ฆฌ์—์„œ ๋งค์šฐ ์ค‘์š”ํ•œ ๋‹จ๊ณ„๋กœ ํ†ต๊ณ„๋‚˜ ์กฐ๊ฐ ์ •๋ณด ๋“ฑ์„ ๋ฐ”ํƒ•์œผ๋กœ ์‹คํ–‰๊ณ„ํš(Execution Plan)์„ ๋งŒ๋“ค์–ด ๋‚ธ๋‹ค. 

  1) ์ฟผ๋ฆฌ ๋ถ„์„: ๊ฒ€์ƒ‰ ์ œํ•œ์ž์ธ์ง€ ์กฐ์ธ ์กฐ๊ฑด์ธ์ง€ ํŒ๋‹จ.

  2) ์ธ๋ฑ์Šค ์„ ํƒ: ๋ถ™ํฌ ํ†ต๊ณ„ ์ •๋ณด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ธ๋ฑ์Šค ๊ฒ€์ƒ‰์ด๋‚˜ ํ…Œ์ด๋ธ” ์Šค์บ” ์ค‘์˜ ํ•˜๋‚˜๋ฅผ ์„ ํƒ. ์—ฌ๋Ÿฌ ์ธ๋ฑ์Šค ์ค‘ ๊ฐ€์žฅ ํšจ์œจ์ ์ธ ์ธ๋ฑ์Šค ํ•˜๋‚˜๋ฅผ ์„ ํƒ

  3) ์กฐ์ธ ์ฒ˜๋ฆฌ: Join, Union, Group By, Order By ์ ˆ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ์ ์ ˆํ•œ ์ž‘์—… ์ˆœ์„œ๋ฅผ ์„ ํƒ

์ดํ›„, ์ถœ๋ ฅ ๊ฐ’์œผ๋กœ ์‹คํ–‰๊ณ„ํš(Excution Plan)์„ ์ถœ๋ ฅํ•œ๋‹ค. 

 

3. Row source Generator

- ์˜ตํ‹ฐ๋งˆ์ด์ € ๊ณผ์ •์„ ํ†ตํ•ด์„œ ๋งŒ๋“ค์–ด์ง„ ์‹คํ–‰๊ณ„ํš์„ ์‹ค์ œ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ ๋˜๋Š” ํ”„๋กœ์‹œ์ € ํ˜•ํƒœ๋กœ ํฌ๋ฉงํŒ… ํ•˜๋Š” ์ž‘์—….

- Row-Source๋Š” ๋ ˆ์ฝ”๋“œ ์ง‘ํ•ฉ์„ ๋ฐ˜๋ณต ์ฒ˜๋ฆฌํ•˜๋ฉด์„œ ์‚ฌ์šฉ์ž๊ฐ€ ์š”๊ตฌํ•œ ์ตœ์ข… ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ์‹ค์ œ์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์ œ์–ด ๊ตฌ์กฐ๋ฅผ ๋งํ•œ๋‹ค.

 

4. ์‹คํ–‰(Execute) : prepared statement๋ฅผ ์“ฐ๋ฉด 1~3 ํ•„์š”์—†๊ณ  4๋งŒ! -> ์„ฑ๋Šฅ ์ข‹์•„์ง


1) Statement stmt ;

2) [ PreparedStatement ; ] ***** ->1~3 ํ•„์š”์—†๊ณ  4๋งŒ! -> ์„ฑ๋Šฅ ์ข‹์•„์ง

private static int selectedNumber;
private static Scanner scanner = new Scanner(System.in);
private static Connection conn ;	
private static PreparedStatement pstmt;  // ์„ ์–ธ
private static ResultSet  rs ;

<๋ณ€๊ฒฝ๋‚ด์šฉ>

* ๋ถ€์„œ์กฐํšŒ selectAllDept

private static void selectAllDept() {
	String sql = "SELECT * "
			+ " FROM dept"
			+ " ORDER BY deptno  ASC";
	
	ArrayList<DeptDTO> deptList = null;
	
	try {
		pstmt = conn.prepareStatement(sql);  // 1  ์ˆ˜์ • (์—ฌ๊ธฐ์„œ sql ๋จผ์ € ๋“ฑ๋กํ•ด์•ผํ•จ)
		rs = pstmt.executeQuery();          // 2 ์ˆ˜์ •   (์—ฌ๊ธฐ์„  sql ๋“ฑ๋กํ•„์š”์—†์Œ)
		
		if( rs.next() ) {
			deptList =  new ArrayList<DeptDTO>();
			do {					
				DeptDTO dto = new DeptDTO(  rs.getInt("deptno") , rs.getString("dname"),  rs.getString("loc"));					
				deptList.add(dto);					
			} while (rs.next());
		} // if
		
		printDept( deptList );
	} catch (SQLException e) { 
		e.printStackTrace();
	} finally {
		try {
			pstmt.close();                                  // 3 ์ˆ˜์ •
			rs.close();
			// DBConn.close();  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒํ•  ๋•Œ ๊ทธ ๋•Œ ํ•œ ๋ฒˆ๋งŒ ๋‹ซ๊ธฐ.
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	} // try
}

 

*๋ถ€์„œ ์ถ”๊ฐ€  insertDept

private static char _continue;  //  'y'  'n'
private static void insertDept() {
		
	do { 
		System.out.println("[๋ถ€์„œ ์ •๋ณด ์ž…๋ ฅ]");
		System.out.print("1. ๋ถ€์„œ๋ช… ์ž…๋ ฅ ? ");
		String pdname = scanner.next();
		System.out.print("2. ์ง€์—ญ๋ช… ์ž…๋ ฅ ? ");
		String ploc = scanner.next();
		// PreparedStatement ๋Š”    ?        ๋ฐ”์ธ๋”ฉ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 
		// (๊ธฐ์–ต)   ๋ฌธ์ž,๋‚ ์งœ   '?'  X
		String sql = "INSERT INTO dept ( deptno, dname, loc ) VALUES ( seq_dept.nextval,  ? , ? )" ;			// 1 ์ˆ˜์ •
		try { 				
			pstmt = conn.prepareStatement(sql);              // 2 ์ˆ˜์ •				
			
			// ?  ?  [IN], OUT, IN OUT ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐ’ ์„ค์ •
			pstmt.setString(1,  pdname);
			pstmt.setString(2,  ploc ) ;
			
			int rowCount = pstmt.executeUpdate();          // 3 ์ˆ˜์ •				
			if( rowCount == 1) {
				System.out.println("> 1๊ฐœ ๋ถ€์„œ ์ถ”๊ฐ€ ์™„๋ฃŒ!!!");
			}
		} catch (SQLException e) { 
			e.printStackTrace();
		} finally {
			try {
				pstmt.close();                                                   // 4 ์ˆ˜์ •
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} // try			 
		๊ณ„์†์—ฌ๋ถ€ํ™•์ธ();
	} while (    Character.toUpperCase(_continue)  == 'Y'  );
}

 

* ๋ถ€์„œ ์ˆ˜์ • udateDept

private static void updateDept() {
System.out.print("> ์ˆ˜์ •ํ•  ๋ถ€์„œ๋ฒˆํ˜ธ(deptno) ์ž…๋ ฅ ? ");
	int pdeptno = scanner.nextInt();
	System.out.print("> ์ˆ˜์ •ํ•  ๋ถ€์„œ๋ช…, ์ง€์—ญ๋ช… ์ž…๋ ฅ ? ");
	String pdname = scanner.next();
	String ploc = scanner.next();
	
	
	String sql =
			               " UPDATE dept "
			            + " SET dname = ? , loc = ? "
			            + " WHERE deptno = ? ";
	
	// 3. UPDATE ์ฒ˜๋ฆฌ
	try {
		pstmt = conn.prepareStatement(sql);
		
		// ? , ? , ? 
		pstmt.setString(1, pdname);
		pstmt.setString(2, ploc);
		pstmt.setInt(3, pdeptno);
		
	    int rowCount =  pstmt.executeUpdate();
	    if( rowCount == 1 ) {
	    	System.out.println("> 1๊ฐœ ๋ถ€์„œ ์ˆ˜์ • ์™„๋ฃŒ!!!");
	    }
	} catch (SQLException e) {
		 
		e.printStackTrace();
	} finally {
		try {
			pstmt.close();
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	}
}

* ๋ถ€์„œ ์‚ญ์ œ deleteDept

private static void deleteDept() {
	System.out.print("> ์‚ญ์ œํ•  ๋ถ€์„œ๋ฒˆํ˜ธ(deptno) ์ž…๋ ฅ ? ");
	int pdeptno = scanner.nextInt();		
	String sql =  " DELETE FROM  dept " 
			            + " WHERE deptno = ? ";  // 1 ์ˆ˜์ •		 
	try {
		pstmt = conn.prepareStatement(sql);// 2 ์ˆ˜์ •
		pstmt.setInt(1, pdeptno);  // 3 ์ถ”๊ฐ€
	    int rowCount =  pstmt.executeUpdate();  // 4 ์ˆ˜์ •
	    if( rowCount == 1 ) {
	    	System.out.println("> 1๊ฐœ ๋ถ€์„œ ์‚ญ์ œ ์™„๋ฃŒ!!!");
	    }
	} catch (SQLException e) {			 
		e.printStackTrace();
	} finally {
		try {
			pstmt.close();// 5 ์ˆ˜์ •
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	}	
}

 

* ๋ถ€์„œ ๊ฒ€์ƒ‰ searchDept

// ๋ถ€์„œ ๊ฒ€์ƒ‰
private static void searchDept() { 
	 
	System.out.println("[ ๊ฒ€์ƒ‰ ์กฐ๊ฑด ]   "); 
	System.out.println("1) ๋ถ€์„œ๋ช…  ");
	System.out.println("2) ์ง€์—ญ๋ช…  ");
	System.out.println("3) ๋ถ€์„œ๋ช… + ์ง€์—ญ๋ช…  ");
	 
	System.out.print("> ๊ฒ€์ƒ‰์กฐ๊ฑด ์„ ํƒ ? ");     // 1,  2, 3
	int searchCondition = scanner.nextInt();
	
	System.out.print("> ๊ฒ€์ƒ‰์–ด ์ž…๋ ฅ ? ");   
	String searchWord = scanner.next();
	
	String sql = " SELECT * "
			         + " FROM dept ";
	
	switch ( searchCondition) {
	case 1: 
		// LIKE
		// sql += " WHERE dname LIKE ? ;		
		// pstmt.setString(1, "%"+searchWord+"%");		
		sql += " WHERE REGEXP_LIKE( dname, ? , 'i' ) ";			
		break;
	case 2:
		sql += " WHERE REGEXP_LIKE( loc, ? , 'i' ) ";			
		break;
	case 3: 
		sql += " WHERE REGEXP_LIKE( dname, ? , 'i' ) OR  REGEXP_LIKE( loc, ?', 'i' ) ";
		break; 
	}		
	sql += " ORDER BY deptno ASC";
	 
	
	// selectAllDept ๋ฉ”์„œ๋“œ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ
    ArrayList<DeptDTO> deptList = null;
		
	try {
		pstmt = conn.prepareStatement(sql);
		
		// 1,2,3 ๊ฒ€์ƒ‰    ?
		pstmt.setString(1, searchWord);			
		// 3 ๊ฒ€์ƒ‰      ?   ? 
		if( searchCondition == 3)   pstmt.setString(2, searchWord);	
		
		rs = pstmt.executeQuery();
		
		if( rs.next() ) {
			deptList =  new ArrayList<DeptDTO>();
			do {					
				DeptDTO dto = new DeptDTO(  rs.getInt("deptno") , rs.getString("dname"),  rs.getString("loc"));					
				deptList.add(dto);					
			} while (rs.next());
		} // if
		
	printDept( deptList );
	} catch (SQLException e) { 
		e.printStackTrace();
	} finally {
		try {
			pstmt.close();
			rs.close();
			// DBConn.close();  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒํ•  ๋•Œ ๊ทธ ๋•Œ ํ•œ ๋ฒˆ๋งŒ ๋‹ซ๊ธฐ.
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	} // try
}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ