2013년 8월 5일 월요일

[Oracle] 자바에서 오라클 stored procedure 호출해서 배열을 변수로 입력



public HashMap callSP (HttpServletRequest req) throws Exception, SQLException {  
Connection con = null;  
PreparedStatement pstmt = null;  
OracleCallableStatement ps = null;  
ResultSet rs = null;  
HashMap hm = new HashMap();                      /*  변수로 던질 배열 생성
                          1차 배열
                     */
String aSingleArray[] = new String[10]; try { con = DBHandler.getConnection();
String RtnCode = ""; String RtnMsg = ""; String RtnCapno = ""; /* 값을 배열에 넣는다. */aSingleArray[0] = req.getParameter("order_no") == null ? "" : req.getParameter("order_no");aSingleArray[1] = req.getParameter("rtncan_dt") == null ? "" : req.getParameter("rtncan_dt");aSingleArray[2] = req.getParameter("ssLoginId") == null ? "" : req.getParameter("ssLoginId"); con.setAutoCommit(false); // 1차 배열의 경우ArrayDescriptor array_desc = ArrayDescriptor.createDescriptor("VARCHAR_ARRAY", con);ARRAY s_array = new ARRAY(array_desc, con, aSingleArray);
ps = (OracleCallableStatement) con.prepareCall({ call PKG_NAME.PROC_NAME(?,?,?)});ps.setArray(1, s_array);ps.registerOutParameter(2, java.sql.Types.VARCHAR); // codeps.registerOutParameter(3, java.sql.Types.VARCHAR); // msg   ps.execute();    // 실행



/* 다차원 배열일 경우 */ 
/* 선언 */        String[][] addArray = new String [bl_no.length][19];
/* 입력 */addArray[i][0] = bl_no[idx] == null ? "" : bl_no[idx];addArray[i][1] = vOrderNo == null ? "" : vOrderNo;addArray[i][2] = acct_cd[idx] == null ? "" : acct_cd[idx];
/* 준비 & 실행 */ArrayDescriptor descriptor =          ArrayDescriptor.createDescriptor( "MULDIM_ARR", con );
ARRAY array_to_pass =  new ARRAY( descriptor, con, addArray );
ps =           (OracleCallableStatement)con.prepareCall           ( " begin PKG_NAME.PROC_BEGIN(?,?,?,?); end;" );
ps.setString(1, ssLoginId);ps.setArray(2, array_to_pass);ps.registerOutParameter(3, java.sql.Types.VARCHAR );ps.registerOutParameter(4, java.sql.Types.VARCHAR );     ps.execute();


댓글 없음: