我想从java发送两个数组到oracle存储过程。 第一个数组是字符串数组,第二个是字符数组 我该怎么做?
答案 0 :(得分:14)
以下是如何操作的示例。
以下脚本在数据库中设置表,类型和存储过程。该过程采用数组类型的参数,并将数组的每一行插入表中:
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
数组。如果我猜对了,那么我认为你最好将char
转换为String
s然后使用与上述相同的方法。
答案 1 :(得分:3)
看这里: 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
答案 2 :(得分:1)
由于自{12}以来不推荐使用ArrayDescriptor
,因此不应再使用它。
这是在12c中为我工作的代码片段:
Array array = ((OracleConnection) connection).createOracleArray("T_VARCHAR2_ARRAY", data);
CallableStatement statement = connection.prepareCall("{call p_array_test(?)}");
statement.setArray(1, array);
statement.execute();
答案 3 :(得分:0)
您可以使用Oracle Types将Java对象映射到Oracle。此外,还有Spring JDBC实用程序。
答案 4 :(得分:0)
正则表达式解决
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;)
答案 5 :(得分:0)
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数据库的要求和类型而有所不同,但基数相同。
我从其他一个答案中复制了这个答案。
Pass array as input parameter to an oracle stored procedure using simple jdbc call