import java.sql.*;
import oracle.jdbc.driver.OracleCallableStatement;
import oracle.jdbc.driver.OracleTypes;
public class RefCursor {
public static void main(String[] args) {
RefCursor vTest = new RefCursor();
vTest.prepareCall();
}
void prepareCall() {
Connection conn = null;
CallableStatement cstmt = null;
OracleCallableStatement ocstmt = null;
try {
String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORA";
String user = "scott";
String password = "tiger";
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
conn = DriverManager.getConnection(url, user, password);
// Stored Procedure 를 호출하기 위해 JDBC Callable Statement를 사용 합니다
String query = null;
query = "{call PROCUDURE_NAME(?, ?,?)}";
cstmt = conn.prepareCall(query);
// 프로시져의 In Parameter로 SELECT문장을 넘깁니다.
int x = 1;
cstmt.setString(x++, "001");
cstmt.setString(x++, "2008");
// CallableStatement를 위한 REF CURSOR OUTPUT PARAMETER를
// OracleTypes.CURSOR로 등록합니다.
int outIndex = x++;
cstmt.registerOutParameter(outIndex, OracleTypes.CURSOR);
// CallableStatement를 실행합니다.
cstmt.execute();
// getCursor() method를 사용하기 위해 CallableStatement를
// OracleCallableStatement object로 바꿉니다.
ocstmt = (OracleCallableStatement) cstmt;
// OracleCallableStatement 의 getCursor() method를 사용해서 REF CURSOR를
// JDBC ResultSet variable 에 저장합니다.
ResultSet cursor = ocstmt.getCursor(outIndex);
//ResultSet cursor = ocstmt.executeQuery();
// 쿼리결과 empno, ename 출력
ResultSetMetaData rsmd = cursor.getMetaData();
int columnCount = rsmd.getColumnCount();
for (int i=0; i<columnCount; i++) {
System.out.print(rsmd.getColumnName(i+1) + "\t");
}
System.out.println("");
int count = 0;
while (cursor.next()) {
System.out.print(count++ + "==>\t");
for (int i=0; i<columnCount; i++) {
System.out.print(cursor.getString(i+1) + "\t");
}
System.out.println("");
}
System.out.println("done!");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
ocstmt.close();
cstmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
댓글 없음:
댓글 쓰기