ndlessrain
2012. 10. 23. 14:25
CallableStatement란?
- CallableStatement interface는 Database에 미리 컴파일 되어 있는 Stored Procedure를 실행하기 위해 사용된다.
CallableStatement 객체는 Connection 객체의 prepareCall() 메소드를 호출하여 얻을 수 있다.
- stored Procedure를 호출하는 형식은 다음과 같다.
Function 호출 |
Procedure 호출 |
{ ? = call [procedur name] [<arg1>, <arg2>, ...] |
{ call [procedur name] [<arg1>, <arg2>, ...] |
{ ? = call pro_name ?, ? , ? } |
{ call sp_cancel_1031 (?, ?, ?, ? ) } |
[표1] 호출 형식
public HashParam proc_state_gb(Connection con, int v_reg_seq, String v_aft_stat){ CallableStatement cs = null; // CallableStatement 객체 선언 HashParam paramsReturn = new HashParam(); String errCode = ""; String message = "";
try{ cs = con.prepareCall("{call sp_cancel_1031(?,?,?,?)}"); // 프로시져(sp_cancel_1031) 호출
cs.setInt(1, v_reg_seq); // sp_cancel_1031 에 넘겨줄 값1 cs.setString(2, v_aft_stat); // sp_cancel_1031 에 넘겨줄 값2 cs.registerOutParameter(3, Types.VARCHAR); // sp_cancel_1031 로부터 받을 값1 cs.registerOutParameter(4, Types.VARCHAR); // sp_cancel_1031 로부터 받을 값2
cs.execute();
errCode = cs.getString(3); // sp_cancel_1031 로부터 받을 값2 message = cs.getString(4);
paramsReturn.setString("code", errCode); // 메소드를 호출한 페이지에 넘길 HashParam 값 셋팅 paramsReturn.setString("msg", message);
}catch(Exception e){ logger.error(message); }finally { DBConnectionManager.close(cs); } return paramsReturn; }
|
[그림] CallableStatement 사용 예