Der er mange måder at sende et array til databasefunktioner på. En enkel er som følger:
Først skal du oprette en TABLE-type i dit DB-schema:
CREATE TYPE DATE_ARRAY AS TABLE OF DATE; Derefter skal du skrive en FUNCTION med denne nye inputtype:
-- a dummy function just for presenting the usage of input array CREATE FUNCTION Date_Array_Test_Function(p_data IN DATE_ARRAY) RETURN INTEGER IS TYPE Cur IS REF CURSOR; MyCur cur; single_date DATE; BEGIN /* Inside this function you can do anything you wish with the input parameter: p_data */ OPEN MyCur FOR SELECT * FROM table(p_data); LOOP FETCH MyCur INTO single_date; EXIT WHEN MyCur%NOTFOUND; dbms_output.put_line(to_char(single_date)); END LOOP; RETURN 0; END Date_Array_Test_Function; Nu kan du i java-koden bruge følgende kode til at kalde en sådan funktion med et array-input:
import java.sql.CallableStatement; import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Types; import oracle.sql.ARRAY; import oracle.sql.ArrayDescriptor; public class Main { public static void main(String[] args) throws SQLException { Connection c = DriverManager.getConnection(url, user, pass); String query = "begin ? := date_array_test_function( ? ); end;"; // note the uppercase "DATE_ARRAY" ArrayDescriptor arrDescriptor = ArrayDescriptor.createDescriptor("DATE_ARRAY", c); // Test dates Date[] inputs = new Date[] {new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis())}; ARRAY array = new ARRAY(arrDescriptor, c, inputs); CallableStatement cs = c.prepareCall(query); cs.registerOutParameter(1, Types.INTEGER); // the return value cs.setObject(2, array); // the input of the function cs.executeUpdate(); System.out.println(cs.getInt(1)); } } Håber, dette er til hjælp. Held og lykke!