使用SimpleJdbcCall将JSON参数传入/传出Oracle存储过程

huangapple go评论84阅读模式
英文:

Pass in/out JSON param to oracle stored procedure using simpleJdbcCall

问题

我在Oracle中有存储过程。以下是其定义:

  1. procedure pName(pObj in out JSON_OBJECT_T)

在dBeaver中调用它的示例:

  1. DECLARE
  2. POBJ JSON_OBJECT_T;
  3. BEGIN
  4. POBJ := JSON_OBJECT_T.parse('{"cli_code" : "01.075648"}');
  5. COLVIR.CPL_PKGDEA_UTL.pGetDealList ( POBJ => POBJ) ;
  6. DBMS_OUTPUT.PUT_LINE(POBJ.stringify());
  7. END;

输出:{"cli_code":"01.075648","cpl_deals":[{"dep_id":1228,"dea_id":14754171,"dea_code":"1","fromdate":"2023-02-17","todate":"2023-12-31","dep_id":1222,"dea_id":14754171,"dea_code":"1","fromdate":"2023-02-17","todate":"2023-12-31"}]}

这是我在Java中的函数:

  1. public void getDealList(String clientCode) {
  2. SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate)
  3. .withSchemaName(SCHEMA_NAME)
  4. .withCatalogName(CPL)
  5. .withProcedureName(pName)
  6. .withoutProcedureColumnMetaDataAccess()
  7. .declareParameters(
  8. new SqlParameter(POBJ, OracleTypes.),
  9. new SqlParameter(POBJ, OracleTypes.JSON));
  10. OracleJsonFactory factory = new OracleJsonFactory();
  11. OracleJsonObject json = factory.createObject();
  12. json.put("cli_code", clientCode);
  13. Map<String, Object> map = new HashMap<>();
  14. map.put(POBJ, json);
  15. SqlParameterSource in = new MapSqlParameterSource(map);
  16. Map<String, Object> out = simpleJdbcCall.execute(in);
  17. }

我一直收到这种类型的错误:java.sql.SQLException: ORA-03115: 不支持的网络数据类型或表示。

如何声明这个输入/输出参数才能使一切正常工作?

我尝试在Oracle文档和Stack Overflow主题中搜索,但没有成功。

英文:

I have stored procedure in Oracle. Here its defenition:

  1. procedure pName(pObj in out JSON_OBJECT_T)

Example of calling it from dBeaver:

  1. DECLARE
  2. POBJ JSON_OBJECT_T;
  3. BEGIN
  4. POBJ := JSON_OBJECT_T.parse(&#39;{&quot;cli_code&quot; : &quot;01.075648&quot;}&#39;);
  5. COLVIR.CPL_PKGDEA_UTL.pGetDealList ( POBJ =&gt; POBJ) ;
  6. DBMS_OUTPUT.PUT_LINE(POBJ.stringify());
  7. END;

OUTPUT: {"cli_code":"01.075648","cpl_deals":[{"dep_id":1228,"dea_id":14754171,"dea_code":"1","fromdate":"2023-02-17","todate":"2023-12-31",
"dep_id":1222,"dea_id":14754171,"dea_code":"1","fromdate":"2023-02-17","todate":"2023-12-31"}]}

Here is my function in java:

  1. public void getDealList(String clientCode) {
  2. SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate)
  3. .withSchemaName(SCHEMA_NAME)
  4. .withCatalogName(CPL)
  5. .withProcedureName(pName)
  6. .withoutProcedureColumnMetaDataAccess()
  7. .declareParameters(
  8. new SqlParameter(POBJ, OracleTypes.),
  9. new SqlParameter(POBJ, OracleTypes.JSON));
  10. OracleJsonFactory factory = new OracleJsonFactory();
  11. OracleJsonObject json = factory.createObject();
  12. json.put(&quot;cli_code&quot;, clientCode);
  13. Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
  14. map.put(POBJ, json);
  15. SqlParameterSource in = new MapSqlParameterSource(map);
  16. Map&lt;String, Object&gt; out = simpleJdbcCall.execute(in);
  17. }

I keep getting this type of error: java.sql.SQLException: ORA-03115: unsupported network datatype or representation.

How should i declare this in/out parameter to make evething work?

I tried to search in oracle documentation/ in stackoverflow threads but there was no success

答案1

得分: 0

JSON_OBJECT_T 是一种 PL/SQL 数据类型,不等同于 JSON,是在你的 Java 代码中由 OracleTypes.JSON 引用的 SQL 数据类型。你需要将你的存储过程封装成一个接受 JSON 并将其转换为 JSON_OBJECT_T 的过程,然后再调用你的原始过程。

英文:

A JSON_OBJECT_T is a PL/SQL datatype not equivalent to JSON, a SQL datatype referenced by OracleTypes.JSON in you Java code. You have to wrap your procedure into one accepting a JSON and converting it into JSON_OBJECT_T to call your original one.

huangapple
  • 本文由 发表于 2023年2月27日 15:44:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定