Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an oracle.sql.ARRAY object?

This question is related to my original issue How to return an array from Java to PL/SQL ?, but is a more specific.

I have been reading Oracle Database JDBC Developer's Guide and

  • Creating ARRAY objects
  • Server-Side Internal Driver
  • oracle.jdbc.OracleConnection
  • oracle.jdbc.OracleDriver

but I still fail to write a minimum code where I can create ARRAY using

ARRAY array = oracle.jdbc.OracleConnection.createARRAY(sql_type_name, elements);

as instructed in Creating ARRAY objects.

I'm using Oracle Database JVM.

I have tried following:

Example 1

create or replace type widgets_t is table of varchar2(32767);
/

create or replace and compile java source named "so20j1" as
public class so20j1 {
    public void f1() {
        String[] elements = new String[]{"foo", "bar", "zoo"};
        oracle.sql.ARRAY widgets =
            oracle.jdbc.OracleConnection.createARRAY("widgets_t", elements);
    }
};
/
show errors java source "so20j1"

Fails with:

Errors for JAVA SOURCE "so20j1":

LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0  so20j1:4: non-static method
     createARRAY(java.lang.String,java.lang.Object) cannot be
     referenced from a static context

0/0  1 error
0/0  ^
0/0  oracle.sql.ARRAY widgets =
     oracle.jdbc.OracleConnection.createARRAY("widgets_t", elements);

Example 2

create or replace type widgets_t is table of varchar2(32767);
/

create or replace and compile java source named "so20j2" as

public class so20j2 {
    public void f1() {
        String[] elements = new String[]{"foo", "bar", "zoo"};
        oracle.jdbc.OracleDriver ora = new oracle.jdbc.OracleDriver();
        java.sql.Connection conn = ora.defaultConnection();
        oracle.sql.ARRAY widgets = conn.createARRAY("widgets_t", elements);
    }
};
/
show errors java source "so20j2"

Fails with:

Errors for JAVA SOURCE "so20j2":

LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0  so20j2:6: cannot find symbol
0/0  symbol  : method createARRAY(java.lang.String,java.lang.String[])
0/0  1 error
0/0  oracle.sql.ARRAY widgets = conn.createARRAY("widgets_t",
     elements);

0/0  ^
0/0  location: interface java.sql.Connection

Disclaimer: I'm not a Java programmer (yet).

like image 681
user272735 Avatar asked Oct 24 '11 16:10

user272735


People also ask

Does Oracle SQL support arrays?

The oracle. sql. ARRAY class contains methods that return array elements as Java primitive types. These methods allow you to access collection elements more efficiently than accessing them as Datum instances and then converting each Datum instance to its Java primitive value.

What is the oracle command to create a Varray?

Oracle SQL commands to create VARRAY types are of the following form: CREATE TYPE typename IS VARRAY(n) OF datatype; Where typename is the desired name of your VARRAY type, n is the desired maximum number of elements in the array, and datatype is the datatype of the array elements.

Can we use array in SQL query?

The ARRAY function returns an ARRAY with one element for each row in a subquery. If subquery produces a SQL table, the table must have exactly one column. Each element in the output ARRAY is the value of the single column of a row in the table.


1 Answers

You're on the right track with #2, but you can't create an oracle Array from a connection of type java.sql.Connection. It has to be an OracleConnection to be able to use those methods.

oracle.jdbc.OracleDriver ora = new oracle.jdbc.OracleDriver();
java.sql.Connection conn = ora.defaultConnection();
OracleConnection oraConn = conn.unwrap(OracleConnection.class);
oracle.sql.ARRAY widgets = oraConn.createARRAY("widgets_t", elements);
like image 93
Affe Avatar answered Sep 23 '22 18:09

Affe