Language/JSP

Procedure Call

적외선 2013. 11. 18. 14:11
// 드라이버 로드
try {
    Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e1) {
    e1.printStackTrace(pw);
}
 
// 연결 클래스 변수 선언
Connection        conn  = null;
CallableStatement cstat = null;
ResultSet         rs    = null;
try {
    conn = DriverManager.getConnection("jdbc:oracle:thin:@아이피:포트:Tnsname", "아이디", "비밀번호");
 
    // 프로시져 콜
    CallableStatement cstat = conn.prepareCall("{CALL 패키지명.프로시져명(?)}");
    // output cursor로 데이터를 받아온다.   
    cstat.registerOutParameter(1, OracleTypes.CURSOR);
    cstat.execute();
 
    rs = (ResultSet)cstat.getObject(1);
     
    if ( rs != null )
    {
        ResultSetMetaData rsmeta = rs.getMetaData();
        int nColumn = rsmeta.getColumnCount();
        out.println("");
        out.println("");
        for (int i = 0; i <= nColumn; i++)
        {
            if ( i == 0 )
                out.println(" 번호 ");
            else
            {
                String strColumnName = rsmeta.getColumnName(i);
                out.println(strColumnName);                
            }
        }//for (int i = 0; i <= nColumn; i++)
        out.println("");
         
        int nRow = 1;
        while (rs.next())
        {
            out.println("");
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i <= nColumn; i++)
            {
                if ( i == 0 )
                    sb.append(  Integer.toString(nRow) );
                else
                    sb.append( rs.getString(i) );
            }
            out.println(sb.toString());
            out.println("");
            nRow++;
        }//while (rs.next())
         
        out.println("");
    }//if ( rs != null )
} catch (SQLException e) {
    e.printStackTrace(pw);
} finally {
    try {rs.close();   } catch (SQLException e) {}
    try {cstat.close();} catch (SQLException e) {}
    try {conn.close(); } catch (SQLException e) {}