'프로시저'에 해당되는 글 2

  1. 2009.12.05 JDBC로 프로시저 호출하기
  2. 2009.05.05 JDBC Code Template - 프로시저

JDBC로 프로시저 호출하기

- SQL Server에서 SELECT 결과를 리턴하는 경우
getDatabaseProductName : Microsoft SQL Server
getDatabaseProductVersion : 9.00.1399
getDriverName : Microsoft SQL Server 2005 JDBC Driver
getDriverVersion : 1.2.2828.100
getDriverVersion(int) : 1.2
getJDBCVersion : 3.0

- 위와 동일한 환경에서 PreparedStatement로 프로시저 실행

[todo] openquery로도 가능하지 않나?

JDBC Code Template - 프로시저

CallableStatement cs = null;
try{
    cs = con.prepareCall("{ ? = call sp_test(?,?,?) }");
    int idx = 0;
    cs.registerOutParameter(++idx, Types.INTEGER);
    cs.setString(++idx, COL_1);
    cs.setString(++idx, COL_2);
    cs.setString(++idx, COL_3);
    
    cs.execute();
    int result = cs.getInt(1);
    if(result == -1){ //check error
        throw new SQLException("result is -1"); //=-=> 좀 더 정형화된 다른 예외를 만들어야...
    }
    return result;

}catch(Exception e){  
    throw new RuntimeSQLException(e, primaryKey); 
}finally{
    if(cs != null){ try{ cs.close(); }catch(Exception ignored){  } }
}