[Day5] JSP/JDBC 5 [11/1]

1. transaction ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ

-- [ ํŠธ๋žœ์žญ์…˜ ์ฒ˜๋ฆฌ ]
-- ์˜ˆ) ๊ณ„์ขŒ ์ด์ฒด =  A   UPDATE , B  UPDATE
--                    ๋…ผ๋ฆฌ์ ์ธ ์ด ๋‘๊ฐœ์˜ ์ž‘์—…์€ ํ•˜๋‚˜๋กœ ๋ฌถ์–ด์…” ๋ชจ๋‘ ์„ฑ๊ณต/ ๋ชจ๋‘ ์‹คํŒจ.
CREATE OR REPLACE PROCEDURE UP_๊ณ„์ขŒ์ด์ฒด
(
       p๊ณ„์ขŒ์ •๋ณด1
     , p๊ณ„์ขŒ์ •๋ณด2
     , p์ด์ฒด๊ธˆ์•ก
)
IS
BEGIN
   UPDATE ํ…Œ์ด๋ธ”๋ช…
   SET ์ž”๊ณ  = ์ž”๊ณ  - p์ด์ฒด๊ธˆ์•ก
   WHERE p๊ณ„์ขŒ์ •๋ณด1;
   
   UPDATE ํ…Œ์ด๋ธ”๋ช…
   SET ์ž”๊ณ  = ์ž”๊ณ  + p์ด์ฒด๊ธˆ์•ก
   WHERE p๊ณ„์ขŒ์ •๋ณด2;
   
   COMMIT;
EXCEPTION
    WHEN THEN
        ROLLBACK;
END;

 

[ํŠธ๋žœ์žญ์…˜ ์‹คํŒจ]

 

Connection conn = null;
conn = DBconn.getConnection();
PreparedStatement psmt = null;
int rowCount = 0;

String sql = "INSERT INTO dept VALUES(?,?,?)";

// 1. deptํ…Œ์ด๋ธ” ๋ถ€์„œ ์ถ”๊ฐ€ O
try{
	pstmt = conn.prepareStatement(sql);
	pstmt.setInt(1,50);
	pstmt.setString(2,"QC");
	pstmt.setString(3,"SEOUL");
	rowCount = pstmt.executeUpdate();
	if(rowCount ==1) System.out.println("> 50/QC/SEOUL ๋ถ€์„œ ์ถ”๊ฐ€ ์„ฑ๊ณต!!!");
} catch (SQLException e) {
	e.printStackTrace();
} finally {
	try {
    	pstmt.close();
    } catch (SQLException e){
    	e.printStackTrace();
    }
}

// 2. deptํ…Œ์ด๋ธ” ๋ถ€์„œ ์ถ”๊ฐ€ X
try{
	pstmt = conn.prepareStatement(sql);
	pstmt.setInt(1,100);
	pstmt.setString(2,"XXX");
	pstmt.setString(3,"YYY");
	rowCount = pstmt.executeUpdate();
	if(rowCount ==1) System.out.println("> 50/QC/SEOUL ๋ถ€์„œ ์ถ”๊ฐ€ ์„ฑ๊ณต!!!");
} catch (SQLException e) {
	e.printStackTrace();
} finally {
	try {
    	pstmt.close();
    } catch (SQLException e){
    	e.printStackTrace();
    }
}

[ํŠธ๋žœ์žญ์…˜ ์„ฑ๊ณต]

conn.setAutoCommit(false); // ์ž๋™์ปค๋ฐ‹ off

conn.commit(); // ์˜ค๋ฅ˜๊ฐ€ ์•ˆ๋‚˜๋ฉด ์ปค๋ฐ‹

conn.rollback(); // ๋กค๋ฐฑ

Connection conn = null;
conn = DBconn.getConnection();
PreparedStatement psmt = null;
int rowCount = 0;

String sql = "INSERT INTO dept VALUES(?,?,?)";

try{
	conn.setAutoCommit(false); // ์ž๋™์ปค๋ฐ‹ off
	// 1๋ฒˆ O
	pstmt = conn.prepareStatement(sql);
	pstmt.setInt(1,50);
	pstmt.setString(2,"QC");
	pstmt.setString(3,"SEOUL");
	rowCount = pstmt.executeUpdate();
	if(rowCount ==1) System.out.println("> 50/QC/SEOUL ๋ถ€์„œ ์ถ”๊ฐ€ ์„ฑ๊ณต!!!");
    // 2๋ฒˆ X
    pstmt = conn.prepareStatement(sql);
	pstmt.setInt(1,100);
	pstmt.setString(2,"XXX");
	pstmt.setString(3,"YYY");
	rowCount = pstmt.executeUpdate();
	if(rowCount ==1) System.out.println("> 50/QC/SEOUL ๋ถ€์„œ ์ถ”๊ฐ€ ์„ฑ๊ณต!!!");
    
    conn.commit(); // ์˜ค๋ฅ˜๊ฐ€ ์•ˆ๋‚˜๋ฉด ์ปค๋ฐ‹
} catch (SQLException e) {
	e.printStackTrace();
    try{
    	conn.rollback(); // ๋กค๋ฐฑ
    } catch (SQLException e1) {
    	e1.printStackTrace();
    }
} finally {
	try {
    	pstmt.close();
    } catch (SQLException e){
    	e.printStackTrace();
    }
}

2. CallableStatement (cstmt) = null

- ์ €์žฅ ํ”„๋กœ์‹œ์ € ์‚ฌ์šฉํ•˜๋Š” stmt

String sql = "{ call ์ €์žฅํ”„๋กœ์‹œ์ €๋ช…(ํŒŒ๋ผ๋ฏธํ„ฐ...)}";

  

* ํ”„๋กœ์‹œ์ € ํ…Œ์ŠคํŠธ

EXEC UP_INSERTEMP (pempno => 9999, pname => 'kim');

* java

psvm() {
	// 1. up_insertemp ์ €์žฅ ํ”„๋กœ์‹œ์ € ํ•˜๋‚˜ ์ƒ์„ฑ
    int empno = 9999;
    String ename = "ADMIN";
    String mgr = 1234;
    int deptno = 10;
    Date hiredate = new Date(1991, 2, 23);
    
    Connection conn = null;
    CollableStatement cstmt = null;
    int rowCount = 0;
    String sql = "{ call UP_INSERTEMP(pempno => ?, pename => ?, pdeptno => ?, phiredate => ? )}";;
    
    conn = DBConn.getConnection();
    
    try{
		cstmt = conn.prepareCall(sql);
        cstmt.setInt(1,empno);
        cstmt.setString(2, ename);
        cstmt.setInt(3,mgr);
        cstmt.setInt(4,deptno);
        cstmt.setDate(5,hiredate);
		rowCount = cstmt.executeUpdate();
        
		if(rowCount ==1) System.out.println("์‚ฌ์› ์ถ”๊ฐ€ ์„ฑ๊ณต!!!");
	} catch (SQLException e) {
		e.printStackTrace();
	} finally {
		try{
        	cstmt.close();
        } catch (SQLException e){
        	e.printStackTrace();
        }
   	 }
}

EX 02_03 ํŒŒ์ผ ๋ฆฌํŒฉํ† ๋ง

1) ์ €์žฅ ํ”„๋กœ์‹œ์ € ๋งŒ๋“ค๊ณ 

2) ์ž๋ฐ” ํŒŒ์ผ ์ƒ์„ฑ

 

- ์ €์žฅ ํ”„๋กœ์‹œ์ € (์‚ญ์ œ)

    ๋ถ€์„œ ๋ฒˆํ˜ธ ์ž…๋ ฅ ๋ฐ›์•„์„œ ๋ถ€์„œ ์‚ญ์ œ

CREATE OR REPLACE PROCEDURE UP_DELETEDEPT
( 
	pdeptno dept.deptno%type
)
IS
BEGIN
	DELETE FROM  dept  
	WHERE deptno = pdeptno;
	COMMIT;
EXCEPTION
WHEN OTHERS THEN
	RAISE_APPLICATION_ERROR(-20011, '> DEPT DELETE X...');
END;

EXEC UP_DELETEDEPT(50);

INSERT INTO dept VALUES ( 50, 'QC', 'SEOUL');
COMMIT;

- ์ž๋ฐ” ํŒŒ์ผ ์ƒ์„ฑ

private static void deleteDept() {
	System.out.print("> ์‚ญ์ œํ•  ๋ถ€์„œ๋ฒˆํ˜ธ(deptno) ์ž…๋ ฅ ? ");
	int pdeptno = scanner.nextInt();      
	String sql = "{ CALL UP_DELETEDEPT( ? ) }";
	try {
		cstmt = conn.prepareCall(sql);
		cstmt.setInt(1, pdeptno);
		int rowCount =  cstmt.executeUpdate();  
		if( rowCount == 1 ) {
			System.out.println("> 1๊ฐœ ๋ถ€์„œ ์‚ญ์ œ ์™„๋ฃŒ!!!");
		}
	} catch (SQLException e) {          
		e.printStackTrace();
	} finally {
		try {
			cstmt.close();
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	} 
}

update

- ํŠน์ • ๋ถ€์„œ ๋ฒˆํ˜ธ์— dname๊ณผ loc ์ˆ˜์ •

- ์ €์žฅ ํ”„๋กœ์‹œ์ € ํŒŒ๋ผ๋ฏธํ„ฐ ๋””ํดํŠธ๊ฐ’ ์„ค์ •๋ฐฉ๋ฒ• ๋‘ ๊ฐ€์ง€

pdname dept.dname%type := null
ploc dept.loc%type DEFAULT NULL
CREATE OR REPLACE PROCEDURE UP_UPDATEDEPT
(
	pdeptno dept.deptno%type
    , pdname dept.dname%type := null
    , ploc dept.loc%type DEFAULT NULL
)
IS
BEGIN
	UPDATE dept
    SET dnmae = NVL(pdname, dname), loc = NVL(ploc, loc)
    WHERE deptno = pdeptno;
    
    COMMIT;
EXCEPTION
	WHEN OTHERS THEN
    	RAISE_APPLICATION_ERROR(-20013, '>DEPT INSERT X...');
END;
// ๋ถ€์„œ ์ˆ˜์ •
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 = { CALL UP_UPDATEDEPT(pdeptno => ?, pdname => ? ploc => ?) };
	
	// System.out.println( sql );
	
	// 1.  2.   
	
	// 3. UPDATE ์ฒ˜๋ฆฌ
	try {
		cstmt = conn.prepareCall(sql);
        cstmt.setInt(1,pdeptno);
	    cstmt.setString(2,pdname);
        cstmt.setString(3,ploc);
        int rowCount =  cstmt.executeUpdate(); 
	    if( rowCount == 1 ) {
	    	System.out.println("> 1๊ฐœ ๋ถ€์„œ ์ˆ˜์ • ์™„๋ฃŒ!!!");
	    }
	} catch (SQLException e) {	 
		e.printStackTrace();
	} finally {
		try {
			stmt.close();
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	}	
	// 4.
}

select

ํ”„๋กœ์‹œ์ €

CREATE OR REPLACE PROCEDURE UP_SELECTDEPT
(
	pcursor OUT SYS_REFCURSOR
)
IS
	vsql VARCHAR2(1000); -- ๋™์ ์ฟผ๋ฆฌ
BEGIN
	vsql := 'SELECT * '
	vsql := vsql || ' FROM dept ';
	
    -- OPEN FOR ๋ฌธ
    OPEN pcursor FOR vsql;
    
    COMMIT;
EXCEPTION
	WHEN OTHERS THEN
    	RAISE_APPLICATION_ERROR(-20013, '>DEPT INSERT X...');
END;

 ์ž๋ฐ”

refcursor -> rs (result set) ์œผ๋กœ ๋ฐ”๋กœ ๋ฐ›์•„์˜ค์ง€ ์•Š์Œ !!

์ปค์„œ๋ฅผ cstmt์— ๋“ฑ๋กํ•˜๊ณ  getObject๋กœ ๋ฐ›์•„์˜ด!

// ์ถœ๋ ฅ์šฉ ๋งค๊ฐœ๋ณ€์ˆ˜ OUT ๋“ฑ๋ก
cstmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
cstmt.executeQuery(); 
		
// ์ถœ๋ ฅ์šฉ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ด
rs = (ResultSet)cstmt.getObject(1);
private static void selectAllDept() {

	String sql { CALL UP_SELECTDEPT((?) };
	
	ArrayList<DeptDTO> deptList = null;
		
	try {
		cstmt = conn.prepareCall(sql);
        // ์ถœ๋ ฅ์šฉ ๋งค๊ฐœ๋ณ€์ˆ˜ OUT ๋“ฑ๋ก
        cstmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
		cstmt.executeQuery(); 
		
        // ์ถœ๋ ฅ์šฉ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ๋ถ€ํ„ฐ ๊ฐ€์ ธ์˜ด
        rs = (ResultSet)cstmt.getObject(1);
        
		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 {
			cstmt.close();
            rs.close();
			DBConn.close();  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒํ•  ๋•Œ ๊ทธ ๋•Œ ํ•œ ๋ฒˆ๋งŒ ๋‹ซ๊ธฐ.
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	} // try
}

id_check ์˜ˆ์‹œ

ํ”„๋กœ์‹œ์ €

create or replace PROCEDURE up_idCheck
(
    pempno  IN emp.empno%type
    ,pempnoCheck OUT NUMBER --  ์‚ฌ์šฉ๊ฐ€๋Šฅ 0,   ์‚ฌ์šฉ๋ถˆ๊ฐ€๋Šฅ 1
)
IS
BEGIN
   SELECT COUNT(*) INTO pempnoCheck
   FROM emp
   WHERE empno = pempno;
-- EXCEPTION
END;

 

์ž๋ฐ”

psvm{
	Syso.("์ค‘๋ณต์ฒดํฌํ•  ์•„์ด๋”” ์ž…๋ ฅ");
    Scanner scanner = new Scanner(System.in);
    int pempno = scanner.nextInt();
    
    Connection conn = null;
    CallableStatement cstmt = null;
    String sql = " {CALL up_idCheck(?,?) }";
    
    conn = DBConn.getConnection();
    try {
		cstmt = conn.prepareCall(sql);
        cstmt.setInt(1,pempno); // IN
        cstmt.registerOutParameter(2, oracle.jdbc.OracleTypes.INTEGER);
        cstmt.executeQuery(); 
        
        int idCheck = (int)cstmt.getObject(2);
        
        if(idCheck == 0) {
        	System.out.println("์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ ์•„์ด๋””");
        } else {
        	System.out.println("์‚ฌ์šฉ์ค‘์ธ ์•„์ด๋””");
        }
	} catch (SQLException e) { 
		e.printStackTrace();
	} finally {
		try {
			cstmt.close();
			DBConn.close();  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒํ•  ๋•Œ ๊ทธ ๋•Œ ํ•œ ๋ฒˆ๋งŒ ๋‹ซ๊ธฐ.
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	} // try
    
}

search

 

-sql-

CREATE OR REPLACE PROCEDURE UP_SEARCHDEPT
( 
     psearchCondition NUMBER  -- 1,2,3
   , psearchword VARCHAR2   --  ๊ฒ€์ƒ‰์–ด
   , pcursor  OUT SYS_REFCURSOR
)
IS
   vsql VARCHAR2(1000);  -- ๋™์  ์ฟผ๋ฆฌ ๋ณ€์ˆ˜ ์„ ์–ธ
BEGIN
    vsql := 'SELECT *  '; 
    vsql :=  vsql || ' FROM dept  '; 
    
    IF psearchCondition = 1 THEN
       vsql :=  vsql || ' WHERE REGEXP_LIKE( dname, :psearchword , ''i'' )'; 
    ELSIF psearchCondition = 2 THEN 
        vsql :=  vsql || ' WHERE REGEXP_LIKE( loc, :psearchword, ''i'' )'; 
    ELSIF psearchCondition = 3 THEN
       vsql :=  vsql || ' WHERE REGEXP_LIKE( dname, :psearchword, ''i'' ) OR  REGEXP_LIKE( loc, :psearchword, ''i'' )'; 
    END IF;
    
    vsql :=  vsql || ' ORDER BY deptno ASC ';
      
    
    -- OPEN~FOR๋ฌธ
    IF psearchCondition = 1 THEN
        OPEN pcursor FOR vsql  
                     USING psearchword; 
    ELSE
     OPEN pcursor FOR vsql 
         -- INTO 
         USING psearchword, psearchword; 
    END IF;
   
    COMMIT;
EXCEPTION
   WHEN OTHERS THEN
       RAISE_APPLICATION_ERROR(-20014, '> DEPT SEARCH  X...');
END;

-java-

private static void searchDept() {
		// ๊ฒ€์ƒ‰์กฐ๊ฑด ์„ ํƒ(์ž…๋ ฅ)    1) ๋ถ€์„œ๋ช…  2) ์ง€์—ญ๋ช…  3) ๋ถ€์„œ๋ช… + ์ง€์—ญ๋ช…
		// ๊ฒ€์ƒ‰์–ด 
	 
		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 = "{CALL UP_SEARCHDEPT(?,?,?)}";
		
        // selectAllDept ๋ฉ”์„œ๋“œ ๋ณต์‚ฌ ๋ถ™์—ฌ๋„ฃ๊ธฐ
		ArrayList<DeptDTO> deptList = null;
		
		try {
			cstmt = conn.prepareCall(sql);
			cstmt.setInt(1, searchCondition);
			cstmt.setString(2, searchWord);
			cstmt.registerOutParameter(3, oracle.jdbc.OracleTypes.CURSOR);
			cstmt.executeQuery(); 
            
            rs = (ResultSet)cstmt.getObject(3);
			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();
			} catch (SQLException e) { 
				e.printStackTrace();
			}
		} // try
	}
}

์ถ”๊ฐ€๋ฌธ์ œ

 

1. ๋ถ€์„œ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ํ•ด๋‹น ๋ถ€์„œ์›๋“ค์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋”ฉ์„ ํ•˜์„ธ์š”. 

    1) CallableStatement ์‚ฌ์šฉ
    2) UP_SELECTDEPTEMP ์ €์žฅ ํ”„๋กœ์‹œ์ € ์ƒ์„ฑ ํ›„ ์‚ฌ์šฉ.
    3) printEmp( List list)  ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•ด์„œ ์ถœ๋ ฅ.

-sql-

CREATE OR REPLACE PROCEDURE UP_SELECTDEPTEMP
(
    pdpateno dept.deptno%type
    , pcursor OUT SYS_REFCURSOR
)
IS
	vsql VARCHAR2(1000);
BEGIN
	vsql := 'SELECT * '
	vsql := vsql || ' FROM  emp  ';
	vsql := vsql || ' WHERE deptno =: pdeptno   ';
	
    -- OPEN FOR ๋ฌธ
    OPEN pcursor FOR vsql
    -- INTO
    USING pdeptno
    
EXCEPTION
	WHEN OTHERS THEN
    	RAISE_APPLICATION_ERROR(-20013, '>EMP SELECT X...');
END;

-java-

public static void main(String[] args) {
    Connection conn = null;
    CallableStatement cstmt = null;
    ResultSet rs = null;
    ArrayList<EmpDTO> list = null;
    String sql = "{CALL UP_SELECTDEPTEMP(?,?)}";
    
    Scanner scanner = new Scanner(System.in);
    int deptno = 0;
    System.out.println("๋ถ€์„œ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”");
    pdeptno = scanner.nextInt();
    
    conn = DBConn.getConnection();
    
    try {
		cstmt = conn.prepareCall(sql);
        cstmt.setInt(1, pdeptno);
        cstmt.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
		cstmt.executeQuery(); 
		 
        rs = (ResultSet)cstmt.getObject(2);
        
        printEmp(list);
        
		if( rs.next() ) {
			list =  new ArrayList<EmpDTO>();
			do {					
				EmpDTO dto = new DeptDTO();
                dto.setEmpno(rs.getInt("empno"));
                dto.setEname(rs.getString("ename"));
                dto.setHiredate(rs.getDate("hiredate"));
                dto.setDeptno(rs.getInt("deptno"));
                
				list.add(dto);					
			} while (rs.next());
		} // if		
		printEmp( list );
	} catch (SQLException e) { 
		e.printStackTrace();
	} finally {
		try {
			cstmt.close();
            rs.close();
			DBConn.close();  
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	} // try
}

private static void printEmp(List<EmpDTO> list) {
	Iterator<EmpDTO> ir = list.Iterator();
    while(ir.hashNext()){
		EmpDTO dto = ir.next();
        System.out.println(dto);
    }
}

2.  ID(empno), PWD(ename) ์„ ์ž…๋ ฅ๋ฐ›์•„์„œ

    ๋กœ๊ทธ์ธ ์„ฑ๊ณต
    ๋กœ๊ทธ์ธ ์‹คํŒจ - ID๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    ๋กœ๊ทธ์ธ ์‹คํŒจ - ID๋Š” ์กด์žฌํ•˜์ง€๋งŒ PWD๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    ๋ผ๊ณ   ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋”ฉ์„ ํ•˜์„ธ์š”.
    1) CallableStatement ์‚ฌ์šฉ
    2) ์˜ค๋ผํด ์ˆ˜์—… ๋•Œ ์ƒ์„ฑํ•œ UP_LOGON ์ €์žฅ ํ”„๋กœ์‹œ์ € ์‚ฌ์šฉ.

-sql-

CREATE OR REPLACE PROCEDURE up_logon
(
    pempno    emp.empno%type
    , pename  emp.ename%type
    , plogonCheck OUT  NUMBER  -- ์„ฑ๊ณต 0  ID X -1            1
)
IS
   vename emp.ename%type;
BEGIN
    SELECT COUNT(*)  INTO plogonCheck -- 1,  0
    FROM emp
    WHERE empno = pempno;
    
    IF plogonCheck = 1  THEN  -- ID ์กด์žฌํ•œ๋‹ค.
        SELECT ename INTO vename
        FROM emp
        WHERE empno = pempno;        
        IF  vename = pename THEN
            plogonCheck := 0;
        ELSE
            plogonCheck := 1;
        END IF;        
    ELSE 
       plogonCheck := -1;
    END IF;
-- EXCEPTION
END;

-java-

private static void id_Check() {
	Connection conn = null;
	CallableStatement cstmt = null;
    String sql { CALL UP_LOGON((?, ?, ?) };
    
    Scanner scanner = new Scanner(System.in);
	
    System.out.print("ID, PW ์ž…๋ ฅ");
    
	int pempno = scanner.nextInt();      
	String pename = scanner.next();      
	
    conn = DBConn.getConnection();
	
	try {
		cstmt = conn.prepareCall(sql);
		cstmt.setInt(1, pempno);
		cstmt.setString(2, pename);
		cstmt.registerOutParameter(3, oracle.jdbc.OracleTypes.INTEGER);
		cstmt.executeQuery(); 
		
		int idCheck = (int)cstmt.getObject(3);
        
		if(idCheck == 0) {
        		System.out.println("๋กœ๊ทธ์ธ ์„ฑ๊ณต");
        	} else if(idCheck == -1) {
        		System.out.println("๋กœ๊ทธ์ธ ์‹คํŒจ, ๋น„๋ฐ€๋ฒˆํ˜ธ ์˜ค๋ฅ˜");
        	} else if(idCheck == 1){
			System.out.println("๋กœ๊ทธ์ธ ์‹คํŒจ, ์•„์ด๋”” ์—†์Œ");
		}
	} catch (SQLException e) { 
		e.printStackTrace();
	} finally {
		try {
			cstmt.close();
			DBConn.close();  
		} catch (SQLException e) { 
			e.printStackTrace();
		}
	} 
}

Reflection

- ResultSet ๊ฒฐ๊ณผ๋ฌผ์— ๋Œ€ํ•œ ์ •๋ณด ์ถ”์ถœํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ˆ 

 

public static void main(String[]args) {
      String sql = "SELECT table_name"
               + "FROM tabs"
               + "ORDER BY table_name ASC";
  
      Connection conn = null;
      PreparedStatement pstmt = null;
      ResultSet rs = null;
      String tableName = null;
      
      conn = DBConn.getConnection();
	
      try {
      	pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        
        int no = 1;
		while(rs.next()) {
        	tableName = rs.getString(1);
            System.out.printf("%d. %s\n", no++, tableName);
        } // while
        // SCOTT์ด ์†Œ์œ ํ•œ ๋ชจ๋“  ํ…Œ์ด๋ธ” ์ถœ๋ ฅ
        
        
      } catch (SQLException e){
      	e.printStackTrace();
      } finally {
      	try {
        	pstmt.close();
        } catch (SQLException e){
        	e.printStackTrace();
        }
      }
      System.out.print(">ํ…Œ์ด๋ธ”๋ช… ์ž…๋ ฅ");
      Scanner scanner = new Scanner(System.in);
      tableName = scanner.next();
      
      sql = "SELECT *"
      	    +" FROM + tableName";
            
      try {
      	pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        
        // getMetaData() : rs๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์ปฌ๋Ÿผ์ •๋ณด(์ˆ˜,์ž๋ฃŒํ˜•, ์ปฌ๋Ÿผ๋ช…) ์–ป์–ด์˜ค๋Š” ๋ฉ”์„œ๋“œ
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        System.out.println(">์นผ๋Ÿผ๊ฐฏ์ˆ˜:" + columnCount);
        
        String columnName = rsmd.getColumnName(1);  // ์ฒซ๋ฒˆ์งธ ์นผ๋Ÿผ๋ช…
        System.out.println(">์นผ๋Ÿผ๊ฐฏ์ˆ˜:" + columnCount);
        
        int columnType = rsmd.getColumnType(1);
        System.out.println(">์นผ๋Ÿผํƒ€์ž…:" + columnType);
        
        String columnTypeName = rsmd.getColumnTypeName(1);
        System.out.println(">์นผ๋Ÿผํƒ€์ž…:" + columnTypeName);
        
        int p = rsmd.getPrecision(1);
        System.out.println(">์ •๋ฐ€๋„:" +p);
        
        int s = rsmd.getScale(1);
        System.out.println(">์Šค์ผ€์ผ:" +s);
        
        for(int i=1; i<columnCount; i++) {
        	String columnName = rsmd.getCountName(i);
            System.out.printf("%s\t", columnName);
        }
        // NUMBER      2(getInt)    Scale != 0 (์‹ค์ˆ˜) (getDouble)
        // VARCHAR2   12(
        // DATE       93
        
        for(int i=1; i<columnCount; i++) {
        	int columnType = rsmd.getColumnType(i);
            int scale = rsmd.getScale(i);
            
            if(columnType == 2 && scale == 0) {
              System.out.printf("%d\t", rs.getInt(i));
            } else if(columnType ==2 && scale != 0) {
              System.out.printf("%2f\t", rs.getDouble(i));
            } else if(columnType == 93) {
              System.out.printf("%tf\t", rs.getDate(i));
            } else if(columnType ==12) {
              System.out.printf("%s\t", rs.getString(i));
        }
      } catch (SQLException e){
      	e.printStackTrace();
      } finally {
      	try {
        	pstmt.close();
        } catch (SQLException e){
        	e.printStackTrace();
        }
      }
      DBConn.close();
      System.out.println("END");

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ