JAVA에서 ORACLE로 배열넘기기

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

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

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

제가 능력많아..떡하니 이렇게 작품을 내놓으면 좋겠지만......
애석하게도.. 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년 전 조회 2,046
17년 전 조회 2,650
17년 전 조회 2,719
17년 전 조회 1,741
17년 전 조회 1,487
17년 전 조회 2,744
17년 전 조회 3,185
17년 전 조회 3,545
17년 전 조회 3,483
17년 전 조회 2,647
17년 전 조회 4,073
17년 전 조회 3,507
17년 전 조회 5,802
17년 전 조회 2,979
17년 전 조회 2,180
17년 전 조회 2,261
17년 전 조회 2,085
17년 전 조회 2,027
17년 전 조회 3,096
17년 전 조회 2,203
17년 전 조회 1,864
17년 전 조회 2,382
17년 전 조회 1,425
17년 전 조회 1,984
17년 전 조회 3,202
17년 전 조회 2,453
17년 전 조회 2,104
17년 전 조회 2,332
17년 전 조회 1,635
17년 전 조회 2,036
17년 전 조회 1,545
17년 전 조회 2,076
17년 전 조회 2,110
17년 전 조회 1,341
17년 전 조회 3,442
17년 전 조회 4,620
17년 전 조회 2,773
17년 전 조회 7,786
17년 전 조회 4,147
17년 전 조회 4,469
17년 전 조회 2,723
17년 전 조회 3,225
17년 전 조회 3,957
17년 전 조회 4,301
17년 전 조회 3,478
17년 전 조회 3,476
17년 전 조회 3,601
17년 전 조회 2,765
17년 전 조회 2,360
17년 전 조회 3,408
17년 전 조회 3,570
17년 전 조회 4,897
17년 전 조회 1,904
17년 전 조회 2,928
17년 전 조회 2,998
17년 전 조회 2,224
17년 전 조회 2,507
17년 전 조회 2,141
17년 전 조회 2,703
17년 전 조회 4,094
17년 전 조회 4,183
17년 전 조회 2,395
17년 전 조회 1,647
17년 전 조회 1,782
17년 전 조회 1,930
17년 전 조회 1,913
17년 전 조회 1,670
17년 전 조회 1,821
17년 전 조회 1,633
17년 전 조회 2,123
17년 전 조회 3,053
17년 전 조회 2,131
17년 전 조회 1,682
17년 전 조회 1,981
17년 전 조회 2,630
17년 전 조회 2,242
17년 전 조회 1,551
17년 전 조회 6,189
17년 전 조회 3,565
17년 전 조회 3,486
17년 전 조회 2,862
17년 전 조회 3,069
17년 전 조회 3,025
17년 전 조회 3,533
17년 전 조회 2,659
17년 전 조회 2,771
17년 전 조회 2,828
17년 전 조회 2,829
17년 전 조회 2,955
17년 전 조회 3,514
17년 전 조회 2,989
17년 전 조회 1,733
17년 전 조회 3,152
17년 전 조회 1,869
17년 전 조회 1,632
17년 전 조회 2,303
17년 전 조회 1,747
17년 전 조회 1,690
17년 전 조회 1,613
17년 전 조회 1,842