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).
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);
Based on answers of Affe and Chris Mazzola I have succeeded to build two examples that compile in Oracle 11g R2 database.
Example based on Affe's answer
create or replace type widgets_t is table of varchar2(32767);
/
create or replace and compile java source named "so20ja1" as
public class so20ja1 {
public void f1() throws java.sql.SQLException {
String[] elements = new String[]{"foo", "bar", "zoo"};
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 widgets = oraConn.createARRAY("widgets_t", elements);
}
};
/
show errors java source "so20ja1"
Example based on Chris Mazzola's answer
create or replace type widgets_t is table of varchar2(32767);
/
create or replace and compile java source named "so20ja2" as
public class so20ja2 {
public void f1() throws java.sql.SQLException {
String[] elements = new String[]{"foo", "bar", "zoo"};
oracle.jdbc.OracleDriver ora = new oracle.jdbc.OracleDriver();
java.sql.Connection conn = ora.defaultConnection();
oracle.sql.ArrayDescriptor desc =
oracle.sql.ArrayDescriptor.createDescriptor("widgets_t", conn);
java.sql.Array widgets = new oracle.sql.ARRAY(desc, conn, elements);
}
};
/
show errors java source "so20ja2"
// array sample (using a stored procedure to sum two or more numbers)
Connection connection = dataSource.getConnection(username,password);
ArrayDescriptor desc =
ArrayDescriptor.createDescriptor(schemaName + "." + arrayType, connection);
// first ? is the array, second ? is the result via out parameter
String sql = "call sum_numbers(?,?)";
CallableStatement cs = connection.prepareCall(sql);
String[] args = {"5","15","25","35"}; // what to sum
Array array = new oracle.sql.ARRAY(desc, connection, args);
cs.setArray(1, array);
cs.registerOutParameter(2, Types.INTEGER);
cs.execute();
int result = cs.getInt(2);
cs.close();
Just to mention that in Java 1.6 you have connection.createArrayOf(..), which is standard.
Map<String, Object> params = new HashMap<>();
params.put("input_to_sp", new ExampleArrayMapper (new String[] { "ABC" }));
Map<String, Object> results = spObject.execute(params);
final List<Object> output = (List<Object>) results.get("po_out_cur");
public class ExampleArrayMapper extends AbstractSqlTypeValue {
private String[] customObject;
public ExampleArrayMapper (String[] customObject) {
this.customObject= customObject;
}
public String getSQlTypeName() throws SQLException {
return "NAME_OF_TYPE_IN_SQL";
}
#Override
protected Object createTypeValue(Connection con, int sqlType, String typeName) throws SQLException {
try {
con = dataSource.getConnection().unwrap(OracleConnection.class);
Array reportsArray = ((OracleConnection) con).createOracleArray(SQL_TYPE_NAME, customObject);
return reportsArray;
} finally {
con.close();
}
}
}
Oracle 12.2 version:
oracle.jdbc.OracleArray v_arr ;
String[] v_list ;
v_arr = ((oracle.jdbc.OracleConnection)DriverManager.getConnection("jdbc:default:connection:")).createARRAY ( "T_STRING_ARRAY" , v_list ) ;
Related
There are types:
CREATE OR REPLACE TYPE my_type IS OBJECT (
id VARCHAR2(20),
name VARCHAR2(40),
phone NUMBER
);
I'm trying to create objects using these types:
....
Object[] myArray = new Object[3];
Object[] struct = new Object[values.size()];
int arrayIndex = 0;
for (User user : values) {
myArray[0] = user.id().toString();
myArray[1] = user.getName().toString();
myArray[2] = user.getPhone();
struct[arrayIndex++] = con.createStruct("my_type",myArray);
}
On the line with the creation of the structure of con.createStruct, an error occurs:
java.sql.SQLException: Inconsistent java and sql object types, for classes implementing ORAData or OracleData, respective factory classes ORADataFactory and OracleDataFactory should be registered in typeMap.
What can be wrong?
myArray[0] = user.id().toString();
myArray[1] = user.getName().toString();
myArray[2] = user.getPhone();
This should be like:
-- First element of the array.
myArray[0].id = user.id().toString();
myArray[0].name = user.getName().toString();
myArray[0].phone = user.getPhone();
You could employ class StructDescriptor from Oracle JDBC extensions:
StructDescriptor structDescriptor = StructDescriptor.createDescriptor("my_type",conn);
Object[] attributes = {user.id().toString(),
user.getName().toString(),
user.getPhone()};
new STRUCT(structDescriptor, connection, attributes);
https://docs.oracle.com/cd/E18283_01/appdev.112/e13995/oracle/sql/StructDescriptor.html
I have a Spring Boot application where I host several REST and SOAP WebServices. The latest request from my client was to execute a stored procedure which receives several parameters and 3 custom arrays.
It seems to work fine, but after 5 executions, I get the following error:
Timeout: Pool empty. Unable to fetch a connection in 30 seconds, none available[size:5; busy:5; idle:0; lastwait:30000].
I don't understand why connections are not being release when using this specific feature.
I'm extending StoredProcedure from Spring:
public class OracleArrayStoredProcedure extends StoredProcedure {
After that, I have a bunch of final Strings for my parameters (over 50), and then I have the constructor:
public OracleArrayStoredProcedure(DataSource ds) {
super(ds, PROC_NAME);
Inside the constructor I have the parameters, which includes also the arrays:
declareParameter(new SqlParameter(PARAM1, OracleTypes.NUMBER));
// Arrays
declareParameter(new SqlInOutParameter(ARRAY1, OracleTypes.ARRAY, "ARRAY1"));
declareParameter(new SqlInOutParameter(ARRAY2, OracleTypes.ARRAY, "ARRAY2"));
declareParameter(new SqlInOutParameter(ARRAY3, OracleTypes.ARRAY, "ARRAY3"));
compile();
Then I have the execute, which is where I get the connection I can't release:
public ArrayResponse execute(ArrayRequest arrayRequest) {
ArrayResponse response = new ArrayResponse();
Map<String, Object> inputs = new HashMap<String, Object>();
try {
OracleConnection connection = getJdbcTemplate().getDataSource().getConnection()
.unwrap(OracleConnection.class);
// ARRAY1
ArrayDescriptor arrayFirstDescriptor = new ArrayDescriptor(ARRAY1, connection);
StructDescriptor recFirstDescriptor = new StructDescriptor("FIRSTREC", connection);
Object[] arrayFirstStruct = new Object[arrayRequest.getArray1().size()];
int i = 0;
for (Iterator<Array1> iterator = arrayRequest.getArray1().iterator(); iterator
.hasNext();) {
Model1 model1 = (Model1) iterator.next();
STRUCT s = new STRUCT(arrayFirstDescriptor, connection, new Object[] {
// Bunch of attributes
arrayStructArray[i++] = s;
}
ARRAY inStructArray = new ARRAY(arrayDescriptor, connection, array1Struct);
finally I put the arrays and parameters in the inputs map and execute it:
inputs.put(ARRAY1, inStructArray);
Map<String, Object> out = super.execute(inputs);
The issue with this approach, is that I can't release the connection (even if I use connection.close()), so after 5 executions it doesn't work anymore. What am I doing wrong?
When I don't have to use a STRUCT, i don't need to use
OracleConnection connection = getJdbcTemplate().getDataSource().getConnection()
.unwrap(OracleConnection.class);
So everything works just fine.
I was able to fix it by implementing AbstractSqlTypeValue(), which has a method that passes the connection of the DataSource. This way, I don't have to manually get a connection. Then I simply add the structArray to the input map.
SqlTypeValue structArray = new AbstractSqlTypeValue() {
#Override
protected Object createTypeValue(Connection connection, int arg1, String arg2) throws SQLException {
Object[] modelArray = new Object[request.getArray().size()];
int i = 0;
for (Iterator<Model> iterator = request.getArray().iterator(); iterator.hasNext();) {
Model model = (Model) iterator.next();
Struct s = connection.createStruct("TEST_REC", new Object[] {
// All attributes go here
});
modelArray[i++] = s;
}
Array structArray = ((OracleConnection) connection).createOracleArray("TEST_STRUCT",
modelArray);
return structArray ;
}
};
In the database are three Oracle custom types (simplified) as follows:
create or replace TYPE T_ENCLOSURE AS OBJECT(
ENCLOSURE_ID NUMBER(32,0),
ENCLOSURE_NAME VARCHAR2(255 BYTE),
ANIMALS T_ARRAY_ANIMALS,
MEMBER FUNCTION CHECK_IF_RED RETURN BOOLEAN
);
create or replace TYPE T_ARRAY_ANIMALS is TABLE OF T_ANIMAL;
create or replace TYPE T_ANIMAL AS OBJECT(
ANIMAL_ID NUMBER(32,0),
NUMBER_OF_HAIRS NUMBER(32,0)
);
and a function, that build the object tree
FUNCTION GET_ENCLOSURE ( f_enclosure_id zoo_schema.ENCLOSURE_TABLE.ENCLOSURE_ID%TYPE ) RETURN T_ENCLOSURE
AS
v_ENC T_ENCLOSURE;
v_idx pls_integer;
BEGIN
v_ENC := T_ENCLOSURE(
f_enclosure_id,
NULL,
T_ARRAY_ANIMALS(T_ANIMAL(NULL,NULL))
);
SELECT ENCLOSURE_NAME
INTO v_ENC.ENCLOSURE_NAME
FROM ENCLOSURE_TABLE WHERE ENCLOSURE_ID = f_ENCLOSURE_ID;
SELECT
CAST(MULTISET(
SELECT ANIMAL_ID, NUMBER_OF_HAIRS
FROM ANIMAL_TABLE
WHERE ENCLOSURE_ID = f_ENCLOSURE_ID
) AS T_ARRAY_ANIMALS
)
INTO v_ENC.ANIMALS
FROM dual;
RETURN v_ENC;
END;
Now I want to call the GET_ENCLOSURE function and work with its result T_ENCLOSURE object in my Java code.
// prepare the call
Connection connection = MyConnectionFactory.getConnection(SOME_CONNECTION_CONFIG);
CallableStatement stmt = connection.prepareCall("{? = call zoo_schema.zoo_utils.GET_ENCLOSURE( ? )}");
stmt.registerOutParameter(1, OracleTypes.STRUCT, "zoo_schema.T_ENCLOSURE");
stmt.setInt(2, 6); // fetch data for ENCLOSURE#6
// execute function
stmt.executeQuery();
// extract the result
Struct resultStruct = (Struct)stmt.getObject(1); // java.sql.Struct
I can access ID and NAME via
Integer id = ((BigInteger)resultStruct.getAttributes()[0]).intValue(); // works for me
String name = (String)resultStruct.getAttributes()[1]); // works for me
However, I cannot seem to get the list of animals
resultStruct.getAttributes()[2].getClass().getCanonicalName(); // oracle.sql.ARRAY
ARRAY arrayAnimals = (ARRAY)jdbcStruct.getAttributes()[2];
arrayAnimals.getArray(); // throws a java.sql.SQLException("Internal Error: Unable to resolve name")
I had a bit of trial and error here including
OracleConnection oracleConnection = connection.unwrap(OracleConnection.class);
STRUCT resultOracleStruct = (STRUCT) stmt.getObject(1); // oracle.sql.STRUCT
oracleConnection.createARRAY("zoo_schema.T_ARRAY_ANIMALS", resultOracleStruct.getAttributes()[2]) // throws an SQLException("Fail to convert to internal representation: oracle.sql.ARRAY#8de7cfc4")
But no luck either.
How can I get the list of animals into a List<TAnimal>?
Create objects that implement java.sql.SQLData. In this scenario, create TEnclosure and TAnimal classes, which both implement SQLData.
Just FYI, in newer Oracle JDBC versions, types such as oracle.sql.ARRAY are deprecated in favor of java.sql types. Although I'm not sure how to write an array (described bellow) using only java.sql API.
When you implement readSQL() you read fields in order. You obtain a java.sql.Array with sqlInput.readArray(). So TEnclosure.readSQL() would look something like this.
#Override
public void readSQL(SQLInput sqlInput, String s) throws SQLException {
id = sqlInput.readBigDecimal();
name = sqlInput.readString();
Array animals = sqlInput.readArray();
// what to do here...
}
Note: readInt() also exists, but Oracle JDBC seems to always provide BigDecimal for NUMBER
You will notice that some APIs such as java.sql.Array have methods that take a type map Map<String, Class<?>> This is a mapping of Oracle type names to their corresponding Java class implementing SQLData (ORAData may work too?).
If you just call Array.getArray(), you will get Struct objects unless the JDBC driver knows about your type mappings via Connection.setTypeMap(typeMap). However, setting typeMap on the connection didn't work for me, so I use getArray(typeMap)
Create your Map<String, Class<?>> typeMap somewhere and add entries for your types:
typeMap.put("T_ENCLOSURE", TEnclosure.class);
typeMap.put("T_ANIMAL", TAnimal.class);
Within a SQLData.readSQL() implementation, call sqlInput.readArray().getArray(typeMap), which returns Object[] where the Object entries or of type TAnimal.
Of course the code to convert to a List<TAnimal> gets tedious, so just use this utility function and adjust it for your needs as far as null vs empty list policy:
/**
* Constructs a list from the given SQL Array
* Note: this needs to be static because it's called from SQLData classes.
*
* #param <T> SQLData implementing class
* #param array Array containing objects of type T
* #param typeClass Class reference used to cast T type
* #return List<T> (empty if array=null)
* #throws SQLException
*/
public static <T> List<T> listFromArray(Array array, Class<T> typeClass) throws SQLException {
if (array == null) {
return Collections.emptyList();
}
// Java does not allow casting Object[] to T[]
final Object[] objectArray = (Object[]) array.getArray(getTypeMap());
List<T> list = new ArrayList<>(objectArray.length);
for (Object o : objectArray) {
list.add(typeClass.cast(o));
}
return list;
}
Writing Arrays
Figuring out how to write an array was frustrating, Oracle APIs require a Connection to create an Array, but you don't have an obvious Connection in the context of writeSQL(SQLOutput sqlOutput). Fortunately, this blog has a trick/hack to get the OracleConnection, which I've used here.
When you create an array with createOracleArray() you specify the list type (T_ARRAY_ANIMALS) for the type name, NOT the singular object type.
Here's a generic function for writing arrays. In your case, listType would be "T_ARRAY_ANIMALS" and you would pass in List<TAnimal>
/**
* Write the list out as an Array
*
* #param sqlOutput SQLOutput to write array to
* #param listType array type name (table of type)
* #param list List of objects to write as an array
* #param <T> Class implementing SQLData that corresponds to the type listType is a list of.
* #throws SQLException
* #throws ClassCastException if SQLOutput is not an OracleSQLOutput
*/
public static <T> void writeArrayFromList(SQLOutput sqlOutput, String listType, #Nullable List<T> list) throws SQLException {
final OracleSQLOutput out = (OracleSQLOutput) sqlOutput;
OracleConnection conn = (OracleConnection) out.getSTRUCT().getJavaSqlConnection();
conn.setTypeMap(getTypeMap()); // not needed?
if (list == null) {
list = Collections.emptyList();
}
final Array array = conn.createOracleArray(listType, list.toArray());
out.writeArray(array);
}
Notes:
At one point I thought setTypeMap was required, but now when I remove that line my code still works, so I'm not sure if it's necessary.
I'm not certain if you should write null or an empty array, but I assumed the empty array is more correct.
Tips on Oracle types
Oracle uppercases everything, so all type names should be uppercase.
You may need to specify SCHEMA.TYPE_NAME if the type isn't in your default schema.
Remember to grant execute on types if the user you are connecting with is not the owner.
If you have execute on the package, but not the type, getArray() will throw an exception when it tries to look for type metadata.
Spring
For developers using Spring, you may want to look at Spring Data JDBC Extensions, which provides SqlArrayValue and SqlReturnArray, which are useful for creating a SimpleJdbcCall for a procedure that takes an array as an argument or returns an array.
Chapter 7.2.1 Setting ARRAY values using SqlArrayValue for an IN parameter explains how to call procedures with array parameters.
As long as a Oracle specific solution is sufficient, the key lies within the DTOs. All of them have to implement ORAData and ORADataFactory
public class TAnimal implements ORAData, ORADataFactory {
Integer animal_id, number_of_hairs;
public TAnimal() { }
// [ Getter and Setter omitted here ]
#Override
public Datum toDatum(Connection connection) throws SQLException {
OracleConnection oracleConnection = connection.unwrap(OracleConnection.class);
StructDescriptor structDescriptor = StructDescriptor.createDescriptor("zoo_schema.T_ANIMAL", oracleConnection);
Object[] attributes = {
this.animal_id,
this.number_of_hairs
};
return new STRUCT(structDescriptor, oracleConnection, attributes);
}
#Override
public TAnimal create(Datum datum, int sqlTypeCode) throws SQLException {
if (datum == null) {
return null;
}
Datum[] attributes = ((STRUCT) datum).getOracleAttributes();
TAnimal result = new TAnimal();
result.animal_id = asInteger(attributes[0]); // see TEnclosure#asInteger(Datum)
result.number_of_hairs = asInteger(attributes[1]); // see TEnclosure#asInteger(Datum)
return result;
}
}
and
public class TEnclosure implements ORAData, ORADataFactory {
Integer enclosureId;
String enclosureName;
List<Animal> animals;
public TEnclosure() {
this.animals = new ArrayList<>();
}
// [ Getter and Setter omitted here ]
#Override
public Datum toDatum(Connection connection) throws SQLException {
OracleConnection oracleConnection = connection.unwrap(OracleConnection.class);
StructDescriptor structDescriptor = StructDescriptor.createDescriptor("zoo_schema.T_ENCLOSURE", oracleConnection);
Object[] attributes = {
this.enclosureId,
this.enclosureName,
null // TODO: solve this; however, retrieving data works without this
};
return new STRUCT(structDescriptor, oracleConnection, attributes);
}
#Override
public TEnclosure create(Datum datum, int sqlTypeCode) throws SQLException {
if (datum == null) {
return null;
}
Datum[] attributes = ((STRUCT) datum).getOracleAttributes();
TEnclosure result = new TEnclosure();
result.enclosureId = asInteger(attributes[0]);
result.enclosureName = asString(attributes[1]);
result.animals = asListOfAnimals(attributes[2]);
return result;
}
// Utility methods
Integer asInteger(Datum datum) throws SQLException {
if (datum == null)
return null;
else
return ((NUMBER) datum).intValue(); // oracle.sql.NUMBER
}
String asString(Datum datum) throws SQLException {
if (datum = null)
return null;
else
return ((CHAR) datum).getString(); // oracle.sql.CHAR
}
List<TAnimal> asListOfAnimals(Datum datum) throws SQLException {
if (datum == null)
return null;
else {
TAnimal factory = new TAnimal();
List<TAnimal> result = new ArrayList<>();
ARRAY array = (ARRAY) datum; // oracle.sql.ARRAY
Datum[] elements = array.getOracleArray();
for (int i = 0; i < elements.length; i++) {
result.add(factory.create(elements[i], 0));
}
return result;
}
}
}
then fetching the data works like so:
TEnclosure factory = new TEnclosure();
Connection connection = null;
OracleConnection oracleConnection = null;
OracleCallableStatement oracleCallableStatement = null;
try {
connection = MyConnectionFactory.getConnection(SOME_CONNECTION_CONFIG);
oracleConnection = connection.unwrap(OracleConnection.class);
oracleCallableStatement = (OracleCallableStatement) oracleConnection.prepareCall("{? = call zoo_schema.zoo_utils.GET_ENCLOSURE( ? )}");
oracleCallableStatement.registerOutParameter(1, OracleTypes.STRUCT, "zoo_schema.T_ENCLOSURE");
oracleCallableStatement.setInt(2, 6); // fetch data for ENCLOSURE#6
// Execute query
oracleCallableStatement.executeQuery();
// Check result
Object oraData = oracleCallableStatement.getORAData(1, factory);
LOGGER.info("oraData is a {}", oraData.getClass().getName()); // acme.zoo.TEnclosure
} finally {
ResourceUtils.closeQuietly(oracleCallableStatement);
ResourceUtils.closeQuietly(oracleConnection);
ResourceUtils.closeQuietly(connection); // probably not necessary...
}
I Just share the logic which worked for me. You can try this to retrieve ARRAY response from PL/SQL to Java.
CallableStatement callstmt = jdbcConnection.prepareCall("{call PROCEDURE_NAME(?, ?)}");
callstmt.setArray(1, array);
callstmt.registerOutParameter(2,Types.ARRAY, <ARRAY_NAME_DECLARED_IN_PL/SQL>);
// Do all execute operations
Array arr = callstmt.getArray(1);
if (arr != null) {
Object[] data = (Object[]) arr.getArray();
for (Object a : data) {
OracleStruct empstruct = (OracleStruct) a;
Object[] objarr = empstruct.getAttributes();
<Your_Pojo_class> r = new <Your_Pojo_class>(objarr[0].toString(), objarr[1].toString());
System.out.println("Response-> : "+ r.toString());
}
}
You could probably cast to java.sql.Array as such:
Object array = ( (Array) resultOracleStruct.getAttributes()[2]) ).getArray();
I'm trying to push "Arraylist" to oracle stored procedure and after making necessary modification, the object it is returned back.
I have an oracle stored procedure with an inout parameter which is "AS TABLE OF TYPE".
I'm able to make the call using Mybatis by implements TypeHandler and overrides its method public void setParameter(PreparedStatement ps, int i, Object parameter,
JdbcType jdbcType) throws SQLException
But I'm facing issue while retrieving the object.
For retrieving I Override the below method.
public Object getResult(CallableStatement cs, int columnIndex)
throws SQLException {
ARRAY array_to_pass = ((OracleCallableStatement) cs).getARRAY(1);
/* showing content */
Datum[] elements = array_to_pass.getOracleArray();
for (int i = 0; i < elements.length; i++) {
Object[] element = ((STRUCT) elements[i]).getAttributes();
String value = (String) element[0];
System.out.println("array(" + i + ").val=" + value);
}
}
I'm getting the below error
java.lang.IllegalAccessError: tried to access class oracle.jdbc.driver.OracleCallableStatement
Statement generating the error is ARRAY array_to_pass = ((OracleCallableStatement) cs).getARRAY(1);
Any thoughts/help on how to retrieve table of type object from oracle .
I'm able to implement it successfully using Spring, Mybatis & Oracle.
The reference code to implement is as below
1) Create Type in Database
CREATE OR REPLACE TYPE TypeName AS OBJECT(
Field1 varchar(6),
Field2 varchar(6)
)
2) Create Table of Type in Database
CREATE OR REPLACE TYPE TableTypeName AS TABLE OF TypeName;
3) Create transfer object in java with 2 fields
4) Generate necessary arraylist for transfer object
5) Create database call
CALL StoredProcedureName(
#{Parameter_in, javaType=Object, jdbcType=ARRAY, jdbcTypeName=TableTypeName , mode=INOUT, typeHandler=javaHandlername}
)
6) Create handler
public class javaHandlername implements TypeHandler {
// Set data to oracle object
#Override
public void setParameter(PreparedStatement ps, int i, Object parameter,
JdbcType jdbcType) throws SQLException
{
List<TO> TOs = (List<TO>) parameter;
StructDescriptor structDescriptor = StructDescriptor
.createDescriptor("TypeName", ps.getConnection());
STRUCT[] structs = null;
structs = new STRUCT[TOs.size()];
for (int index = 0; index < TOs.size(); index++) {
TO to = TO.get(index);
Object[] params = new Object[2];
params[0] = fSVDisptchSchedDely.getField1();
params[1] = fSVDisptchSchedDely.getField2();
STRUCT struct = new STRUCT(structDescriptor,
ps.getConnection(), params);
structs[index] = struct;
}
ArrayDescriptor desc = ArrayDescriptor.createDescriptor(
"TableTypeName", ps.getConnection());
ARRAY oracleArray = new ARRAY(desc, ps.getConnection(), structs);
ps.setArray(i, oracleArray);
}
// Set the result back to Java object
public Object getResult(CallableStatement cs, int columnIndex)
throws SQLException {
List<TO> TOs= new ArrayList<TO>();
Object[] structArray = (Object[]) cs.getArray(columnIndex).getArray();
oracle.sql.STRUCT mystruct = null;
for (Object structObj : structArray) {
TO to= new TO();
mystruct = (oracle.sql.STRUCT) structObj;
Object[] structAttr = mystruct.getAttributes();
to.setField1((String)structAttr[0]));
to.setField2((String)structAttr[1]));
TOs.add(to);
}
return TOs;
}
}
7) Make the database call
Enjoy... :)
In the DB have one collection:
CREATE OR REPLACE TYPE TY_Test as object
(
ID NUMBER,
ACCOUNT NUMBER,
PRIORITY CHAR(20),
)
CREATE OR REPLACE TYPE TY_Test_Instance as table of TY_Test
And TY_Test_Instance is passed to one procedure.
Below is Java code:
public static void main(String[] args) throws Exception {
List<Object> list = new ArrayList<Object>();
String[] objs = new String[3];
objs[0] = new String("863");
objs[1] = new String("1");
objs[2] = new String("3");
Connection conn = DBHelper.getInstance().conn;
StructDescriptor sDescriptor = StructDescriptor.createDescriptor("TY_Test", conn);
list.add(new STRUCT(sDescriptor, conn, objs));
Object[] obj_array = list.toArray();
ArrayDescriptor aDescriptor = ArrayDescriptor.createDescriptor("TY_Test_Instance", conn);
ARRAY array = new ARRAY(aDescriptor, conn, obj_array);
CallableStatement cstm = conn.prepareCall("{call PR_TEST(?,?)}");
cstm.setInt(1, 123);
cstm.setArray(2, array);
cstm.registerOutParameter(1, Types.INTEGER);
cstm.execute();
if (cstm.getInt(1) != 0) {
System.out.println("done");
}
}
when execute the script, it pop ups java.lang.ClassCastException: oracle.sql.NUMBER cannot be cast to oracle.sql.CHAR in the code of cstm.setArray(2, array);
I have spent much time but have not got any solution. Please help on it.