问题
ArrayDescriptor arrayDescriptor =
ArrayDescriptor.createDescriptor("NUMBER_ARRAY", conn);
if (parameter != null) {
ARRAY oracleArray =
new ARRAY(arrayDescriptor, conn, intList.toArray());
ps.setArray(i, oracleArray);
}
else {
ps.setNull(i, Types.ARRAY, "NUMBER_ARRAY");
}
In the above code I was trying to set oracleArray the to null. It does'nt throw any exception. But Oracle doesnt take it as null I guess. What could be the way to pass in a null, I mean to set a list to null what a orcle DB could take it as NUMBER_ARRAY(null) which will be later passed to a Stored Procedure(SP). The SP is ready to take a null list. If it is a null list it returns nothing. And if it is not a null list it will give the results. this is a valid scenario.
回答1:
It seems you have some confusion about null arrays, empty arrays and arrays that contain only a single NULL value.
A NULL array is the absence of an array, in the same way that a NULL number is the absence of a number. An empty array is an array that exists, but has 0 elements in it. Both are different from NUMBER_ARRAY(null), which is an array that contains a single NULL value.
The COUNT method on an array, which returns the number of elements in the array, provides an illustration of the differences between these three.
Firstly, a NULL array:
SQL> declare
2 l_null_array number_array := null;
3 begin
4 dbms_output.put_line('Count: ' || l_null_array.COUNT);
5 end;
6 /
declare
*
ERROR at line 1:
ORA-06531: Reference to uninitialized collection
ORA-06512: at line 4
Here, we get an error. We can't find out how many elements there are in l_null_array because we don't have an array to find the number of elements of.
Secondly, an empty array:
SQL> declare
2 l_empty_array number_array := number_array();
3 begin
4 dbms_output.put_line('Count: ' || l_empty_array.COUNT);
5 end;
6 /
Count: 0
PL/SQL procedure successfully completed.
Here, we can find the number of elements in an empty array, and that number is zero.
Finally, an array containing only NULL:
SQL> declare
2 l_array_containing_null number_array := number_array(null);
3 begin
4 dbms_output.put_line('Count: ' || l_array_containing_null.COUNT);
5 end;
6 /
Count: 1
PL/SQL procedure successfully completed.
This array has one element within it, and that one element is NULL.
Note that you can pass as many arguments as you like to the NUMBER_ARRAY constructor function, and these values will be the initial contents of the array. For example, NUMBER_ARRAY(1, 4, 18, 11, 22, 6) creates a number array with 6 elements in it.
So, how can we set each kind of array using JDBC?
To set a
NULLarray, useps.setNull(i, Types.ARRAY, "NUMBER_ARRAY");as you have done above.
For an empty array, use:
ps.setArray(i, new ARRAY(arrayDescriptor, conn, new Integer[0]));For an array containing a single
NULLvalue only, useps.setArray(i, new ARRAY(arrayDescriptor, conn, new Integer[] { null }));
I'm using an Integer array in these examples, but other numeric types should work too.
来源:https://stackoverflow.com/questions/29310082/preparedstatement-setting-null-for-number-array-doesnt-work