JAVA에서 ORACLE로 배열넘기기

· 17년 전 · 3316
오라클과 프로그램간에 배열을 어떻게 주고 받을 것이지
고민해 보신 분들이 계실 줄 압니다.

며칠간 시간이 나서 이리저리 찾아보니까..다음처럼 결과물이 나왔네요...
사실 몇 달 전부터 한 번 봐야 쓰겄다 했는데...이제사 조금 감을 잡았습니다.
실제적으로 프로젝트에 적용해본 적은 없고..테스트로 해 본 내용입니다.

필요한 상황에 맞게 응용을 해보시면 될 성 싶습니다.

제가 능력많아..떡하니 이렇게 작품을 내놓으면 좋겠지만......
애석하게도.. http://asktom.oracle.com/pls/ask/f?p=4950:1:  에서 자료참조를 했습니다.


1. Java Stored Procedure "ArrayDemo" 생성

create or replace
and compile java source named "ArrayDemo"
as
    import java.io.*;
    import java.sql.*;
    import oracle.sql.*;
    import oracle.jdbc.driver.*;
 
    public class ArrayDemo
    {
       public static void passArray() throws SQLException
       {
           Connection conn = new OracleDriver().defaultConnection();
 
           int intArray[] = { 1,2,3,4,5,6 };
 
           ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor( "NUM_ARRAY",
conn);
           ARRAY array_to_pass = new ARRAY( descriptor, conn, intArray );
 
           OraclePreparedStatement ps =
                 (OraclePreparedStatement)conn.prepareStatement( "begin give_me_an_array
(:x); end;" );
           ps.setARRAY( 1, array_to_pass );
           ps.execute();
       }
    }
/

Java created.


2. 사용할 Array(or TYPE) 생성
create or replace type NUM_ARRAY as table of number;
/

Type created.


3. Array 출력시키는 Procedure 생성
create or replace procedure give_me_an_array( p_array in num_array )
as
begin
    for i in 1 .. p_array.count
    loop
      dbms_output.put_line( p_array(i) );
      end loop;
    end;
/

Procedure created.


4. java source를 호출하는 Procedure 생성
create or replace procedure show_java_calling_plsql
as language java
name 'ArrayDemo.passArray()';
/

Procedure created.


5. Procedure "give_me_an_array"에서 출력하는 내용을 보기 위한 명령어
set serveroutput on


6. show_java_calling_plsql 실행
exec show_java_calling_plsql

결과>
1
2
3
4
5
6
[이 게시물은 관리자님에 의해 2011-10-31 16:47:36 Oracle에서 이동 됨]
|
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
17년 전 조회 1,943
17년 전 조회 2,556
17년 전 조회 2,630
17년 전 조회 1,630
17년 전 조회 1,381
17년 전 조회 2,653
17년 전 조회 3,084
17년 전 조회 3,446
17년 전 조회 3,397
17년 전 조회 2,555
17년 전 조회 3,977
17년 전 조회 3,404
17년 전 조회 5,709
17년 전 조회 2,878
17년 전 조회 2,083
17년 전 조회 2,159
17년 전 조회 1,997
17년 전 조회 1,944
17년 전 조회 3,004
17년 전 조회 2,112
17년 전 조회 1,787
17년 전 조회 2,294
17년 전 조회 1,335
17년 전 조회 1,889
17년 전 조회 3,101
17년 전 조회 2,352
17년 전 조회 2,020
17년 전 조회 2,257
17년 전 조회 1,537
17년 전 조회 1,934
17년 전 조회 1,456
17년 전 조회 1,986
17년 전 조회 2,003
17년 전 조회 1,254
17년 전 조회 3,363
17년 전 조회 4,527
17년 전 조회 2,696
17년 전 조회 7,695
17년 전 조회 4,054
17년 전 조회 4,372
17년 전 조회 2,644
17년 전 조회 3,127
17년 전 조회 3,868
17년 전 조회 4,211
17년 전 조회 3,388
17년 전 조회 3,364
17년 전 조회 3,522
17년 전 조회 2,690
17년 전 조회 2,280
17년 전 조회 3,317
17년 전 조회 3,481
17년 전 조회 4,816
17년 전 조회 1,794
17년 전 조회 2,833
17년 전 조회 2,905
17년 전 조회 2,123
17년 전 조회 2,413
17년 전 조회 2,058
17년 전 조회 2,598
17년 전 조회 4,000
17년 전 조회 4,105
17년 전 조회 2,305
17년 전 조회 1,553
17년 전 조회 1,682
17년 전 조회 1,831
17년 전 조회 1,825
17년 전 조회 1,576
17년 전 조회 1,722
17년 전 조회 1,538
17년 전 조회 2,035
17년 전 조회 2,958
17년 전 조회 2,042
17년 전 조회 1,583
17년 전 조회 1,891
17년 전 조회 2,535
17년 전 조회 2,167
17년 전 조회 1,453
17년 전 조회 6,107
17년 전 조회 3,495
17년 전 조회 3,400
17년 전 조회 2,786
17년 전 조회 2,990
17년 전 조회 2,945
17년 전 조회 3,449
17년 전 조회 2,567
17년 전 조회 2,690
17년 전 조회 2,744
17년 전 조회 2,740
17년 전 조회 2,866
17년 전 조회 3,418
17년 전 조회 2,909
17년 전 조회 1,640
17년 전 조회 3,062
17년 전 조회 1,780
17년 전 조회 1,547
17년 전 조회 2,220
17년 전 조회 1,673
17년 전 조회 1,609
17년 전 조회 1,544
17년 전 조회 1,769