CallableStatement - Stored Procedure 호출
[ CallableStatement ]
CallableStatement : 스토어드프로시저(Stored Procedure)를 실행시키기 위해 사용되는 인터페이스.
---------------------------------------------------------------------------------------------
먼저 간단한 프로시저를 생성.
CREATE or replace PROCEDURE simpleProc(i_id in varchar, v_id out varchar, v_nm out varchar)
is
id number;
nm varchar(20);
begin
SELECT id, name
into v_id, v_nm
FROM TEST1
WHERE id = i_id;
END simpleProc;
---------------------------------------------------------------------------------------------
[소스내용]
package testPackage;
import java.sql.*;
public class CallableStatementTest{
public static void main(String[] args) throws SQLException{
Connection con = null;
CallableStatement cs = null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection ("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "test", "test");
//1개의 in값과 2개의 out값을 받는다.
cs = con.prepareCall("{call simpleProc(?,?,?)}");
cs.setString(1, "6"); //In
cs.registerOutParameter(2, java.sql.Types.VARCHAR); //out
cs.registerOutParameter(3, java.sql.Types.VARCHAR); //out
cs.execute();
System.out.println("id : "+ cs.getString(2) +" name : "+ cs.getString(3));
}catch(Exception e){System.out.println(e);
}finally{
cs.close();
con.close();
}
}
}