传递数组到oracle过程

eulz3vhy  于 2023-10-16  发布在  Oracle
关注(0)|答案(7)|浏览(139)

我想把两个数组从java发送到oracle存储过程。第一个数组是字符串数组,第二个是字符数组,我怎么做呢??

jckbn6z7

jckbn6z71#

这里有一个如何做到这一点的例子。
下面的脚本在数据库中设置一个表、一个类型和一个存储过程。该过程接受数组类型的参数,并将数组的每一行插入表中:

CREATE TABLE strings (s VARCHAR(4000));

CREATE TYPE t_varchar2_array AS TABLE OF VARCHAR2(4000);
/

CREATE OR REPLACE PROCEDURE p_array_test(
    p_strings      t_varchar2_array
)
AS
BEGIN
  FOR i IN 1..p_strings.COUNT
  LOOP
    INSERT INTO strings (s) VALUES (p_strings(i));
  END LOOP;
END;
/

然后,Java代码演示了如何将数组传递到这个存储过程中:

import java.sql.*;
import oracle.jdbc.*;
import oracle.sql.*;

public class ArrayTest {
    public static void main(String[] args) throws Exception {
        DriverManager.registerDriver(new OracleDriver());
        Connection conn = DriverManager.getConnection(
            "jdbc:oracle:thin:@localhost:1521:xe", "user", "pass");

        CallableStatement stmt = conn.prepareCall("BEGIN p_array_test(?); END;");

        // The first parameter here should be the name of the array type.
        // It's been capitalised here since I created it without using
        // double quotes.
        ArrayDescriptor arrDesc =
            ArrayDescriptor.createDescriptor("T_VARCHAR2_ARRAY", conn);

        String[] data = { "one", "two", "three" };
        Array array = new ARRAY(arrDesc, conn, data);
        stmt.setArray(1, array);
        stmt.execute();

        conn.commit();
        conn.close();
    }
}

如果先运行SQL脚本,再运行Java类,然后查询表strings,您应该会发现所有数据都已插入到表中。
当你说“一个字符数组”时,我猜你指的是一个Java char s数组。如果我猜对了,那么我认为最好将char s转换为String s,然后使用与上面相同的方法。

x0fgdtte

x0fgdtte2#

请看:http://download.oracle.com/docs/cd/B19306_01/java.102/b14355/oraarr.htm#i1058512
这是我的一个简短的例子:
1)对数据库

SQL> create or replace type string_array as table of varchar2(100);
  2  /

Type created.

SQL> create or replace function to_string(p_array in string_array) return varchar2
  2  as
  3     l_string varchar2(32767);
  4     i binary_integer;
  5  begin
  6     i := p_array.first();
  7     while i is not null loop
  8        l_string := l_string || p_array(i) || ';';
  9        i := p_array.next(i);
 10     end loop;
 11     l_string := rtrim(l_string, ';');
 12     return l_string;
 13  end;
 14  /

Function created.

2)在java中

public class ArrayTest {
    public static void main(String[] args) throws SQLException {
        DriverManager.registerDriver(new OracleDriver());
        OracleConnection connection = (OracleConnection) DriverManager.getConnection(...);

        String[] elements = {"abc", "def", "geh"};
        ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("STRING_ARRAY", connection);
        ARRAY array = new ARRAY(descriptor, connection, elements);

        OracleCallableStatement stmt = (OracleCallableStatement) connection.prepareCall("{? = call to_string(?)}");
        stmt.registerOutParameter(1, Types.VARCHAR);
        stmt.setARRAY(2, array);
        stmt.execute();

        String result = stmt.getString(1);
        System.out.println("to_string returned: " + result);
    }
}

似乎工作:输出说,

to_string returned: abc;def;geh
p5cysglq

p5cysglq3#

由于ArrayDescriptor自12c以来已被弃用,因此不应再使用它。下面是我在12c中使用的代码片段:

Array array = ((OracleConnection) connection).createOracleArray("T_VARCHAR2_ARRAY", data);
            CallableStatement statement = connection.prepareCall("{call p_array_test(?)}");
            statement.setArray(1, array);
            statement.execute();
s3fp2yjn

s3fp2yjn4#

正则表达式求解

select * from table_a  a where a.col in (select   regexp_substr('SMITH,ALLEN,WARD,JONES','[^,]+', 1, level) from dual
connect by regexp_substr('SMITH,ALLEN,WARD,JONES', '[^,]+', 1, level) is not null;)
qq24tv8q

qq24tv8q5#

PeudoCode也是这样实现的。

# 1.You will require a structDescriptor object for an object equivalent in pl sql like :

    StructDescriptor structDes= new StructDescriptor("<schemaname in caps>.<sql_object_name>", connectionObject);

    # 2. You will need to pass one object values such name, class, id to an object array in order and accordance to 'sql_object_name' object. 

    For exmaple:
    STRUCT[] structArray=new STRUCT[<ListObj>.size()];
    int index=0;
    for (a in ListObj){

    Object[] object=new Object[]{a.getName(),a.getId()};
    STRUCT struct=new STRUCT(structDes ,connectionObject,object);
               structArray[index]=struct;
               index++;

    }

    ArrayDescriptor arrayDes=ArrayDescriptor.createDescriptor(
        "<Schema name>.<table object from sql>", connectionObject);

    ARRAY array=new ARRAY(arrayDes,connectionObject, structArray);

   then pass it to proc 

   .declareParameters(
   new SqlInOutParameter("<parameter to proc name>",OracleTypes.ARRAY,"
   <schema name>.<sql_array_or_table_obj>"))

   like 
   Hashmap<String, Object> map= new HashMap<>();
   map.put("<parameter to proc name>",array);
   psStatement.execute(map);

希望有帮助。这个顺序可能会根据使用的SQL数据库的要求和类型而有所不同,但基础是相同的。
这个答案是从我的另一个答案中抄来的。
使用简单的JDBC调用将数组作为输入参数传递给Oracle存储过程

vpfxa7rd

vpfxa7rd6#

新的方法是将OracleConnection展开,然后使用createOracleArray

OracleConnection oracleConnection = getJdbcTemplate().getDataSource().getConnection().unwrap(OracleConnection.class);
Array array = oracleConnection.createOracleArray("REF_TYPE_ARRAY", referralTypeId);

inputParameters.put("itemIds", array);

另请参阅:如何创建一个oracle.sql.ARRAY对象?

w6mmgewl

w6mmgewl7#

可以使用Oracle类型将Java对象Map到Oracle。此外,还有Spring JDBC实用程序。

相关问题