0

I have the following list of Person Objects:

List<Person> persons;

The Person object is as follows:

@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Person
{
    @XmlElement
    private int id;
    @XmlElement
    private String name;

    public Person(){

    }

}

A Stored Procedure I am invoking takes a list of Person Objects as an Oracle ARRAY.

How can I convert my list of Java Objects into an Oracle ARRAY in order to pass it into the Stored Procedure?

I have seen the CallableStatement.setArray() method, but I do not know how to convert my Java List into an Oracle ARRAY first.

java123999
  • 6,974
  • 36
  • 77
  • 121
  • Also duplicate of http://stackoverflow.com/questions/3626061/how-to-call-oracle-stored-procedure-which-include-user-defined-type-in-java – MT0 Dec 12 '16 at 09:48

1 Answers1

0

You can create an sql Array using the method createARRAY.

Creates an ARRAY object with the given type name and elements.

Here a simple example

    String[] array = new String[]{"a", "b", "c"};
    oracle.jdbc.OracleDriver ora = new oracle.jdbc.OracleDriver();
    java.sql.Connection conn = ora.defaultConnection();
    oracle.jdbc.OracleConnection oraConn = (oracle.jdbc.OracleConnection) conn;
    java.sql.Array sqlArray = oraConn.createARRAY("typeName", array);

If you start from a List of Object

List<Object> list = ...
...
Object[] array = list.toArray();
oracle.jdbc.OracleDriver ora = new oracle.jdbc.OracleDriver();
java.sql.Connection conn = ora.defaultConnection();
oracle.jdbc.OracleConnection oraConn = (oracle.jdbc.OracleConnection) conn;
java.sql.Array sqlArray = oraConn.createARRAY("typeName", array);

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56