我正在尝试将Oracle过程调用的输出参数转换为对象。由于我需要定义一个映射来告诉方法如何进行转换,所以它无法正常工作。如果映射为空或未正确填充,则默认为类型为STRUCT的对象 - 在我的情况下是错误的。
我建立了一个示例,应该说明这个问题:
由于错误的地图,我无法使用类似以下内容的功能:
我遇到了一个异常:
我建立了一个示例,应该说明这个问题:
-- Procedure in database
PROCEDURE myprocedure (
inputParam IN VARCHAR2 (4),
outputOne OUT outputOneSQLType
outputTwo OUT outputTwoSQLType);
-- inside of a package
inside a package mypackage
-- first type
create or replace
TYPE outputOneSQLType IS TABLE OF tableOneExample
-- table of type
create or replace
TYPE tableOneExample AS OBJECT (
somethingOne VARCHAR2 (4)
,somethingTwo NUMBER (12)
)
//java from here
import oracle.jdbc.OracleCallableStatement;
import oracle.jdbc.OracleTypes;
import oracle.jdbc.oracore.OracleTypeADT;
import oracle.sql.STRUCT;
...
oracle.jdbc.driver.OracleConnection oracleConn = (oracle.jdbc.driver.OracleConnection) con.getMetaData().getConnection();
final OracleCallableStatement storedProc = (OracleCallableStatement)oracleConn.prepareCall("{call mypackage.myprocedure("+
":inputParam, :outputOne, :outputTwo)}");
storedProc.setString("inputParam", "SomeValue");
storedProc.registerOutParameter("outputOne", OracleTypes.STRUCT, "OUTPUTONESQLTYPE");
storedProc.registerOutParameter("outputTwo", OracleTypes.STRUCT, "OUTPUTTWOSQLTYPE");
storedProc.execute();
//So far so good
//now I am lost - I need to define the map to get the object?
//What should be inside the map?
Hashtable newMap = new Hashtable();
newMap.put("outputOneSQLType", ?????.class);
//If the map is empty, it defaults to STRUCT...
final STRUCT myObject = (STRUCT)storedProc.getObject("somethingOne",newMap);
// myObject.getBytes() gives me an object... but I cannot cast it to anything
由于错误的地图,我无法使用类似以下内容的功能:
final MyClass myObject = (MyClass)storedProc.getObject("somethingOne",newMap);
如何填写地图?
编辑 1
无法更改 Oracle 数据库中的条目。我只允许使用它们。因此,
stmt.registerOutParameter(2, java.sql.Types.ARRAY, "OUTPUTONESQLTYPE");
无法工作。因为一旦我不使用“OracleTypes.STRUCT”,就会抛出异常。看起来在outputOneSQLType中有一个类型为“OracleTypeCOLLECTION”的对象。
当我尝试使用
Hashtable newMap = new Hashtable();
newMap.put("outputOneSQLType",OracleTypeCOLLECTION.class);
final OracleTypeCOLLECTION myObject = (OracleTypeCOLLECTION)storedProc.getObject("somethingOne",newMap);
我遇到了一个异常:
InstantiationException: oracle.jdbc.oracore.OracleTypeCOLLECTION
@DazzaL:我会尝试定义一个SQLDATA接口,也许那就是解决方案。
解决方案:
- 准备语句应该类似于"begin package.procedure(...); end;"而不是"{call package.procedure(...)})"
- 需要一个SQLData接口。