有类型:
CREATE OR REPLACE TYPE my_type IS OBJECT (
id VARCHAR2(20),
name VARCHAR2(40),
phone NUMBER
);
我正在尝试使用以下类型创建对象:
....
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);
}
在创建con.createStruct结构的行上,发生错误:
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.
有什么不对?
答案 0 :(得分:1)
myArray[0] = user.id().toString();
myArray[1] = user.getName().toString();
myArray[2] = user.getPhone();
这应该是:
-- First element of the array.
myArray[0].id = user.id().toString();
myArray[0].name = user.getName().toString();
myArray[0].phone = user.getPhone();
答案 1 :(得分:0)
您可以使用Oracle JDBC扩展中的类StructDescriptor
:
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