简体   繁体   English

如何在java中映射TYPE TABLE OF VARCHAR2(5)?

[英]How to map TYPE TABLE OF VARCHAR2(5) in java?

I have a procedure with the signature below. 我有一个带有下面签名的程序。

CREATE OR REPLACE PACKAGE BODY MYSCHEMA.MYPACK
AS
PROCEDURE GETBOX (DSSO_BoxNumber      IN     VARCHAR2,
CreateDateTime         OUT tCreateDateTime,
                          ReceiptDateTime        OUT tReceiptDateTime,
                          CSCBoxNumber           OUT tCSCBoxNumber,
                          DSSOBoxNumber          OUT tDSSOBoxNumber,
                          PackID                 OUT tPackID,
                          RequestID              OUT tRequestID,
                          ExceptionID            OUT tExceptionID,
                          Name                   OUT tName,
                          FolderID               OUT tFolderID,
                          ClosedDateTime         OUT tClosedDateTime,
                          OpenStatus             OUT tOpenStatus,
                          RequestOpenStatus      OUT tRequestOpenStatus,
                          RETURNED               OUT tRETURNED)
...

The cutom type definitions are as below. cutom类型定义如下。

CREATE OR REPLACE PACKAGE MYSCHEMA.MYPACK
  AS
      TYPE tCreateDateTime is TABLE of  VARCHAR2(15)
      INDEX BY BINARY_INTEGER;
      TYPE tReceiptDateTime is TABLE of VARCHAR2(15)
      INDEX BY BINARY_INTEGER;
      TYPE tCSCBoxNumber is TABLE of VARCHAR2(20)
      INDEX BY BINARY_INTEGER;
      TYPE tDSSOBoxNumber is TABLE of  VARCHAR2(20)
      INDEX BY BINARY_INTEGER;
      TYPE tPackID is TABLE of VARCHAR2(20)
      INDEX BY BINARY_INTEGER;
      TYPE tRequestID is TABLE of VARCHAR2(20)
      INDEX BY BINARY_INTEGER;
      TYPE tExceptionID is TABLE of  VARCHAR2(20)
...

Can anyone please help, how to register the out parameters in java? 任何人都可以请求帮助,如何在java中注册out参数?

I have tried the following, but no luck. 我试过以下,但没有运气。

cs.setString(1, "XYZ123");
cs.registerOutParameter(2, Types.ARRAY,"MYSCHEMA.MYPACK.tCreateDateTime");
...

Getting the below error. 得到以下错误。

java.sql.SQLException: invalid name pattern: MYSCHEMA.MYPACK.tCreateDateTime
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.oracore.OracleTypeADT.initMetadata(OracleTypeADT.java:463)
at oracle.jdbc.oracore.OracleTypeADT.init(OracleTypeADT.java:362)
at oracle.sql.ArrayDescriptor.initPickler(ArrayDescriptor.java:1756)
at oracle.sql.ArrayDescriptor.<init>(ArrayDescriptor.java:272)
...

We have checked that the schema has access to the procedure and it is defined in the master schema. 我们已检查架构是否可以访问该过程,并且它在主模式中定义。 We also have tried by creating public synonym for this package. 我们也试过为这个包创建公共同义词。 Still not working.. 还是行不通..

The title of your question is misleading. 你的问题的标题是误导性的。 table of <TYPE> and table of <TYPE> index by <TYPE> are two very different data types. table of <TYPE> table of <TYPE> index by <TYPE>是两种截然不同的数据类型。 First is called nested table and second is called associative array in Oracle (PL/)SQL parlance. 第一个称为嵌套表 ,第二个在Oracle(PL /)SQL术语中称为关联数组

The main problems are that: 主要问题是:

  • the collection types used in PL/SQL interfaces revealed to Java needs to be SQL types, not PL/SQL types 在Java中显示的PL / SQL接口中使用的集合类型需要是SQL类型,而不是PL / SQL类型
  • an associative array is not a SQL type but a PL/SQL type 关联数组不是SQL类型,而是PL / SQL类型

The first issue is addressed eg in How to return an array from Java to PL/SQL? 第一个问题在例如如何将数组从Java返回到PL / SQL中得到解决? (the issue is the same even the call direction is different). (即使呼叫方向不同,问题也是一样的)。

Further reading: 进一步阅读:

I was able to map a type table of varchar2 to java as follows: 我能够将varchar2的类型表映射到java,如下所示:

  1. Create a new type outside of any PLSQL package and grant necessary permissions. 在任何PLSQL包之外创建一个新类型并授予必要的权限。

      CREATE OR REPLACE TYPE STRARRAY AS TABLE OF VARCHAR2 (100); / GRANT all ON MYSCHEMA.STRARRAY TO MYUSER1; / commit; 
  2. Create a PLSQL function that accepts/returns the strarray. 创建一个接受/返回strarray的PLSQL函数。 This was declared in the package specification and written in full in the package body. 这是在包规范中声明的,并在包体中完整写入。 Although I declared the array to be IN OUT, in my implementation I only actually use the output from the PLSQL call. 虽然我声明数组是IN OUT,但在我的实现中我实际上只使用了PLSQL调用的输出。

     PROCEDURE getArr(arr_var IN OUT strarray) IS counter NUMBER := 1; BEGIN arr_var := new strarray(); WHILE counter <= 10 LOOP arr_var.extend(); arr_var(counter) := 'my data string'; END LOOP; END getArr; 
  3. Call the procedure in java. 在java中调用该过程。 In this example the variable conn is of data type Connection and has already been initialized. 在此示例中,变量conn的数据类型为Connection,并且已经初始化。 I am running a jdbc thin client against an Oracle database. 我正在针对Oracle数据库运行jdbc瘦客户端。

     CallableStatement proc = null; String sql = "{ call myPackage.getArr(?) }"; try{ proc = conn.prepareCall(sql); proc.registerOutParameter(1, OracleTypes.Array, "MYSCHEMA.STRARRAY"); proc.execute(); Array arrOut = proc.getArray(1); for (int num=0; num<10; num++){ System.out.println(arrOut[num]); }finally{ proc.close(); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用@Procedure 注释在 Spring JPA 中映射 TYPE TABLE OF VARCHAR2? - How to map TYPE TABLE OF VARCHAR2 in Spring JPA with @Procedure annotation? 使用 java SimpleJdbcCall 调用过程 oracle 没有参数类型“is table of varchar2” - Call procedure oracle with out parameters type “is table of varchar2” using java SimpleJdbcCall 有没有一种简单的方法可以将varchar2(1)(“ 1”或“ 0”)映射到JPA布尔类型 - Is there a easy way to map varchar2(1) (“1” or “0”) to JPA boolean type 如何使用 Hibernate 避免从 VARCHAR 到 VARCHAR2 的隐式类型转换? - How to avoid implicit type conversion from VARCHAR to VARCHAR2 using Hibernate? Oracle Varchar2(byte)与Java字节 - Oracle Varchar2(byte) vs java bytes Apache Spark with Java,从Oracle中的Varchar2转换为日期类型失败 - Apache Spark with Java, converting to Date Type from Varchar2 in Oracle fails 如何映射主键是VARCHAR2类型并执行我的方法来生成它 - How to mapping primary key is VARCHAR2 type and excute my method to generate it 由于更多的大小要求,更改 oracle 中的 varchar2 数据类型 - changing varchar2 data type in oracle due to more size requirement java为oracle VARCHAR2返回空字符串值 - java returns empty String value for oracle VARCHAR2 使用CLOB和VARCHAR2参数从PL / SQL调用Java函数 - Calling a Java function from PL/SQL with CLOB and VARCHAR2 Parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM