`
wuhuizhong
  • 浏览: 684369 次
  • 性别: Icon_minigender_1
  • 来自: 中山
社区版块
存档分类
最新评论

Passing an ARRAY from Java to PL/SQL

 
阅读更多

CREATE OR REPLACE TYPE STRARRAY AS TABLE OF VARCHAR2 (255)
/

CREATE OR REPLACE PACKAGE DEMO_PASSING_PKG
AS
   -- Varchar2's are most easily mapped to the java String type
   PROCEDURE PASS (
      P_IN IN VARCHAR2,
      P_OUT OUT VARCHAR2)
   AS
      LANGUAGE JAVA
         NAME 'demo_passing_pkg.pass( java.lang.String,
                                      java.lang.String[] )';
  
     PROCEDURE PASS (
        P_IN IN STRARRAY,
        P_OUT OUT STRARRAY)
     AS
        LANGUAGE JAVA
           NAME 'demo_passing_pkg.pass_str_array( oracle.sql.ARRAY,
                                               oracle.sql.ARRAY[] )';

   FUNCTION RETURN_STRING
      RETURN VARCHAR2
   AS
      LANGUAGE JAVA
         NAME 'demo_passing_pkg.return_string() return  
           java.lang.String';
END DEMO_PASSING_PKG;
/

 

CREATE OR REPLACE AND COMPILE
JAVA SOURCE NAMED "demo_passing_pkg"
AS
import java.io.*;
import java.sql.*;
import java.math.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;

public class demo_passing_pkg extends Object{

public static void pass( java.lang.String p_in,
                         java.lang.String[] p_out ){

    /*
     * the simplest of datatypes -- the String.  If you remember
     * the C version with 6 formal parameters, null indicators,
     * strlen's, strcpy's and so on -- this is trivial in
     * comparision
     */

    if ( p_in != null ){
        
    System.out.println
        ( "The first parameter is " + p_in.toString() );

        p_out[0] = p_in.toUpperCase();

        System.out.println
        ( "Set out parameter to " + p_out[0].toString() );
    }
}

private static void show_array_info( oracle.sql.ARRAY p_in )
throws SQLException{

    System.out.println( "Array is of type      " +
                         p_in.getSQLTypeName() );
    System.out.println( "Array is of type code " +
                         p_in.getBaseType() );
    System.out.println( "Array is of length    " +
                         p_in.length() );
}

public static void
pass_str_array( oracle.sql.ARRAY p_in, oracle.sql.ARRAY[] p_out )
throws java.sql.SQLException,IOException{

    show_array_info( p_in );
    String[] values = (String[])p_in.getArray();

    for( int i = 0; i < p_in.length(); i++ )
        System.out.println( "p_in["+i+"] = " + values[i] );

    Connection conn = new OracleDriver().defaultConnection();
    ArrayDescriptor descriptor =
       ArrayDescriptor.createDescriptor( p_in.getSQLTypeName(), conn );

    p_out[0] = new ARRAY( descriptor, conn, values );

}

public static String return_string(){
    return "Hello World";
}

}

 

SET serveroutput on size 1000000
EXEC dbms_java.set_output( 1000000 )

DECLARE
   L_IN                          STRARRAY := STRARRAY ();
   L_OUT                         STRARRAY := STRARRAY ();
BEGIN
   FOR I IN 1 .. 5
   LOOP
      L_IN.EXTEND;
      L_IN (I) := 'Element ' || I;
   END LOOP;

   DEMO_PASSING_PKG.PASS (L_IN, L_OUT);

   FOR I IN 1 .. L_OUT.COUNT
   LOOP
      DBMS_OUTPUT.PUT_LINE ('l_out('|| I || ') = ' || L_OUT (I) );
   END LOOP;
END;
/
 

 

 

o STRARRAY is simply our Oracle type that represents the array (collection) we want to 
pass.

o DEMO_PASSING_PKG is our "binding" to the java.  Maps the SQL types to the Java types.

o when you run the PLSQL block at the bottom of your question, we are calling the code:

public static void
pass_str_array( oracle.sql.ARRAY p_in, oracle.sql.ARRAY[] p_out )
throws java.sql.SQLException,IOException{

    show_array_info( p_in );
    String[] values = (String[])p_in.getArray();

    for( int i = 0; i < p_in.length(); i++ )
        System.out.println( "p_in["+i+"] = " + values[i] );

    Connection conn = new OracleDriver().defaultConnection();
    ArrayDescriptor descriptor =
       ArrayDescriptor.createDescriptor( p_in.getSQLTypeName(), conn );

    p_out[0] = new ARRAY( descriptor, conn, values );

}

That code just 

1) dumps the array meta data -- type name, length and so on.
2) gets the array of java strings from the parameter (p_in.getArray())
3) prints out each string in turn (system.out.println)
4) creates a new array to be returned (first half of example shows how to PASS IN, second 
half shows how to pass OUT).  
5) Then, it copies the values in the array we want to return into the OUT parameter.

 

 

分享到:
评论

相关推荐

    11g_plsql_user_guide_and_reference.pdf

    The PL/SQL native compiler generates native code directly, bypassing the intermediate bytecode generation step. This results in faster execution times and reduced memory usage. The native code is ...

    OracleAndXML

    SELECT column_value FROM XMLTable('/root/element' PASSING xmltype('&lt;root&gt;&lt;element&gt;Value1&lt;/element&gt;&lt;element&gt;Value2&lt;/element&gt;&lt;/root&gt;') COLUMNS value VARCHAR2(50) PATH '.'); ``` 4. **XSLT ...

    Java邮件开发Fundamentals of the JavaMail API

    jGuru has been dedicated to promoting the growth of the Java technology community through evangelism, education, and software since 1995. You can find out more about their activities, including ...

    Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams

    Reactive Streams in Java explains how to manage the exchange of stream data across an asynchronous boundary―passing elements on to another thread or thread-pool―while ensuring that the receiving ...

    XML导入Oracle表

    3. 编写PL/SQL过程:为了从XML数据中提取信息,可能需要编写PL/SQL过程来解析XML。可以使用Oracle的内置XML处理函数如XMLTYPE,XMLELEMENT,XMLFOREST等。 4. 使用SQL*Loader加载数据:最后,运行SQL*Loader命令,...

    Passing arguments to Python functions

    ### Python 函数传参详解 在 Python 编程语言中,函数是代码组织的重要组成部分,而函数参数则是函数与外部程序交互的关键。本文将详细解释 Python 函数中的三种不同的参数传递方式:无星号参数、单星号参数(*args...

    JSP Simple Examples

    ArrayIndexOutOfBoundException is thrown when we have to indicate that an array has been accessed with an illegal index. printStackTrace in jsp printStackTrace is a method of the Throwable class. By ...

    passing-l-array-to-a-function.rar_多维传递函数_数组、函数

    在编程中,数组是一种存储多个同类型数据的集合,而函数则是执行特定任务的代码块。当涉及到多维数组,如二维数组(行与列组成的矩阵),在处理复杂问题时非常有用。本主题将深入探讨如何在编程环境中,特别是在C++...

    Java语言程序设计基础篇课后题答案-Chapter18BinaryI_O.pdf

    you would typically create an instance of `FileInputStream`, passing the file path as a parameter, then call the `read()` method repeatedly to read bytes from the file. The `read()` method returns ...

    google-java-format

    google-java-format is a program that reformats Java source code to comply with Google Java Style. The formatter can act on whole files, on limited lines, on specific offsets, passing through to ...

    A Textbook of Java Programming

    This book proposes to be an integration of the theoretical aspects of java programming complemented with the implementation of the same in real world situations. The book would be all encompassing ...

    EMC-AN103-RDS_gigabit PHY layout.pdf

    这份标题为"EMC-AN103-RDS_gigabit PHY layout.pdf"的文档主要讨论了在设计网卡电路板时,如何通过电路板布局来优化EMI/EMC(电磁干扰和电磁兼容)性能。Broadcom作为知名品牌,其提供的这款10/100和千兆PHY(物理层...

    Starting Out with Java Early Objects 5th 第5版 pdf 0分

    11. 数组的使用(Accessing Array Elements in a Loop, Passing an Array to a Method) 掌握如何在循环中访问和操作数组元素。学习如何将数组作为参数传递给方法,并在方法中操作数组。 12. 方法返回对象...

    并行计算导论 第2版An Introduction to Parallel Programming.rar

    2. **并行编程模型**:书中的内容深入到并行编程模型,如OpenMP、MPI(Message Passing Interface)和Pthreads等。这些模型在实际应用中广泛使用,读者将学习如何利用它们实现并行算法。 3. **同步与通信**:并行...

    2-Meter Vertical Dipole Array

    collinear arrays and stacked element arrays, with gains ranging from 3 to 9 dB1. The higher gain antennas have a lower vertical beamwidth. For example, a typical 4-element exposed dipole offset array ...

    Exam Ref 70-761 Querying Data with Transact-SQL

    Exam Ref 70-761 Querying Data with Transact-SQL by Itzik Ben-Gan English | 4 Apr. 2017 | ASIN: B06Y21QGGQ | 352 Pages | AZW3 | 23.79 MB Prepare for Microsoft Exam 70-761–and help demonstrate your ...

    matlab转换java代码-utl_passing_sas_macro_variables_to_R_and_retrieving_macr

    matlab转换java代码utl_passing_sas_macro_variables_to_R_and_retrieving_macro_variables_from_R 将宏变量传递给R并从R中检索宏变量。关键字:sas sql连接合并大数据分析宏oracle teradata mysql sas社区...

    An End-to-End Network for Generating Social Relationship Graphs.pdf

    To achieve this, one computational approach for representing human relationships and attributes is to use an explicit knowledge graph, which allows for high-level reasoning. We introduce a novel end-...

Global site tag (gtag.js) - Google Analytics