<tfoot id='CFppl'></tfoot>
    • <bdo id='CFppl'></bdo><ul id='CFppl'></ul>

    <small id='CFppl'></small><noframes id='CFppl'>

  • <i id='CFppl'><tr id='CFppl'><dt id='CFppl'><q id='CFppl'><span id='CFppl'><b id='CFppl'><form id='CFppl'><ins id='CFppl'></ins><ul id='CFppl'></ul><sub id='CFppl'></sub></form><legend id='CFppl'></legend><bdo id='CFppl'><pre id='CFppl'><center id='CFppl'></center></pre></bdo></b><th id='CFppl'></th></span></q></dt></tr></i><div id='CFppl'><tfoot id='CFppl'></tfoot><dl id='CFppl'><fieldset id='CFppl'></fieldset></dl></div>
    <legend id='CFppl'><style id='CFppl'><dir id='CFppl'><q id='CFppl'></q></dir></style></legend>

        如何从 Java 类中调用带有 out 参数作为表类型的过程

        时间:2023-11-02
          <tbody id='PGGH3'></tbody>
        <legend id='PGGH3'><style id='PGGH3'><dir id='PGGH3'><q id='PGGH3'></q></dir></style></legend>

          <tfoot id='PGGH3'></tfoot>

              <bdo id='PGGH3'></bdo><ul id='PGGH3'></ul>
              <i id='PGGH3'><tr id='PGGH3'><dt id='PGGH3'><q id='PGGH3'><span id='PGGH3'><b id='PGGH3'><form id='PGGH3'><ins id='PGGH3'></ins><ul id='PGGH3'></ul><sub id='PGGH3'></sub></form><legend id='PGGH3'></legend><bdo id='PGGH3'><pre id='PGGH3'><center id='PGGH3'></center></pre></bdo></b><th id='PGGH3'></th></span></q></dt></tr></i><div id='PGGH3'><tfoot id='PGGH3'></tfoot><dl id='PGGH3'><fieldset id='PGGH3'></fieldset></dl></div>

              1. <small id='PGGH3'></small><noframes id='PGGH3'>

                  本文介绍了如何从 Java 类中调用带有 out 参数作为表类型的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想从 Java 代码中调用包 ult_pkg 中的这个过程 get_data_Q1 并显示输出:

                  I want to call this procedure get_data_Q1 in the package ult_pkg from Java code and display the output:

                  CREATE OR REPLACE PACKAGE  ult_pkg
                  AS
                  TYPE t_all_record is record (
                      x_object_type_id        number,
                      x_object_name           varchar2(100),
                      x_object_id             varchar2(70),
                      x_audit_timestamp       timestamp(6),
                      x_payload               clob
                  );
                  
                  --table type to hold table data after querying
                  
                  type tt_all_tab is table of t_all_record index by binary_integer;
                  
                  --declaration pocedures
                  procedure get_data_Q1(x_object_id in varchar2 , x_all_type out tt_all_tab );
                  
                  
                  end ult_pkg;
                  
                  /
                  
                  --body of 'ult_pkg'  package
                  create or replace package body ult_pkg 
                  AS
                  
                      --procedure taking 'object_id' as input parameter and gives out table of 't_all_record' type 
                      procedure get_data_Q1(x_object_id in varchar2 , x_all_type out tt_all_tab )
                      AS
                  
                          i number:=0;
                      begin
                  
                      for r in 
                          ( 
                              SELECT
                              O.object_type_id,O.object_name,O.object_id,A.audit_timestamp,P.payload
                              FROM
                              APPLICATION APP, EXCEPTIONS E,MASTER_AUDIT A,MODULE_TYPE M,OBJECT_TYPE O,PAYLOAD P 
                              WHERE 
                              ( A.MODULE_TYPE_ID = M.MODULE_TYPE_ID ) AND ( M.APPLICATION_ID = APP.APPLICATION_ID ) AND ( A.OBJECT_TYPE_ID = O.OBJECT_TYPE_ID ) AND ( O.OBJECT_ID = x_object_id )
                          )
                      -- loop to asign the data from cursor 'r' to carasponding table type columns
                          loop
                  
                  
                          x_all_type(i).x_object_type_id:=r.object_type_id;
                          x_all_type(i).x_object_name:=r.object_name;
                          x_all_type(i).x_object_id:=r.object_id;
                          x_all_type(i).x_audit_timestamp:=r.audit_timestamp;
                          x_all_type(i).x_payload:=r.payload;
                          i:=i+1;
                          end loop;
                  
                      end get_data_Q1;
                  end ult_pkg;
                  /
                  

                  推荐答案

                  这不可能,参见 访问 PL/SQL 索引表:

                  Oracle JDBC 不支持 RAW、DATE 和 PL/SQL RECORD 作为元素类型.

                  Oracle JDBC does not support RAW, DATE, and PL/SQL RECORD as element types.

                  我可能会像这样使用自定义(全局,而不是包)对象类型:

                  I'd probably use a custom (global, not package) object type like so:

                  CREATE TYPE t_all_record AS OBJECT (
                    x_object_type_id        number,
                      x_object_name           varchar2(100),
                      x_object_id             varchar2(70),
                      x_audit_timestamp       timestamp(6),
                      x_payload               clob
                  )
                  /
                  CREATE TYPE t_all_records IS TABLE OF t_all_record
                  /
                  

                  引用包中的类型表(t_all_records 而不是 tt_all_tab)并像这样填充

                  reference the table of type in your package (t_all_records instead of tt_all_tab) and fill it like so

                  procedure get_data_Q1(x_object_id in varchar2 , x_all_type out t_all_records )
                  AS
                  begin
                      SELECT t_all_record(O.object_type_id,O.object_name,O.object_id,A.audit_timestamp,P.payload)
                      BULK COLLECT INTO x_all_type
                      FROM APPLICATION APP, EXCEPTIONS E,MASTER_AUDIT A,MODULE_TYPE M,OBJECT_TYPE O,PAYLOAD P 
                      WHERE ( A.MODULE_TYPE_ID = M.MODULE_TYPE_ID ) AND ( M.APPLICATION_ID = APP.APPLICATION_ID ) AND ( A.OBJECT_TYPE_ID = O.OBJECT_TYPE_ID ) AND ( O.OBJECT_ID = x_object_id )
                  end get_data_Q1;
                  

                  结果可以像这样从java中使用:

                  Result will be useable from java like so:

                  package tests.jdbc;
                  
                  import java.sql.Array;
                  import java.sql.CallableStatement;
                  import java.sql.Connection;
                  import java.sql.DriverManager;
                  import java.sql.ResultSetMetaData;
                  import java.sql.Struct;
                  import java.sql.Types;
                  
                  import oracle.sql.StructDescriptor;
                  
                  public class OracleTableOfResult {
                      public static void main(String...a) throws Exception {
                          Class.forName("oracle.jdbc.OracleDriver");
                          Connection connection = DriverManager.getConnection("jdbc:oracle:thin:<USER>/<PASS>@<DATABASEHOST>:1521:<SERVICE>");
                  
                          final String typeName = "T_ALL_RECORD";
                          final String typeTableName = "T_ALL_RECORDS";
                  
                          // Get a description of your type (Oracle specific)
                          final StructDescriptor structDescriptor = StructDescriptor.createDescriptor(typeName.toUpperCase(), connection);        
                          final ResultSetMetaData metaData = structDescriptor.getMetaData();
                  
                          // Call the procedure (or whatever else) that returns the table of a custom type
                          CallableStatement cs = connection.prepareCall("{call ult_pkg.get_data_Q1(?, ?)}");
                          cs.setString(1, "the_id");
                          // Result is an java.sql.Array...
                          cs.registerOutParameter(2, Types.ARRAY, typeTableName);     
                          cs.execute();
                  
                          // ...who's elements are java.sql.Structs
                          Object[] data = (Object[]) ((Array) cs.getObject(2)).getArray();
                          for(Object tmp : data) {
                              Struct row = (Struct) tmp;
                              // Attributes are index 1 based...
                              int idx = 1;
                              for(Object attribute : row.getAttributes()) {               
                                  System.out.println(metaData.getColumnName(idx) + " = " + attribute);                                            
                                  ++idx;
                              }
                              System.out.println("---");
                          }
                          cs.close();     
                          connection.close();
                      }
                  }
                  

                  但最终,当您可以在简单的 sql 语句中将查询用作准备好的语句时,是否值得付出努力是值得怀疑的……

                  But in the end, it's questionable if it's worth the effort when you could use your query in a plain sql statement as a prepared statement…

                  这篇关于如何从 Java 类中调用带有 out 参数作为表类型的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Oracle 到 Excel - PL/SQL 导出程序 下一篇:Oracle 中的 SQL Server APPLY 相当于什么?

                  相关文章

                • <legend id='3ngZU'><style id='3ngZU'><dir id='3ngZU'><q id='3ngZU'></q></dir></style></legend>
                  <i id='3ngZU'><tr id='3ngZU'><dt id='3ngZU'><q id='3ngZU'><span id='3ngZU'><b id='3ngZU'><form id='3ngZU'><ins id='3ngZU'></ins><ul id='3ngZU'></ul><sub id='3ngZU'></sub></form><legend id='3ngZU'></legend><bdo id='3ngZU'><pre id='3ngZU'><center id='3ngZU'></center></pre></bdo></b><th id='3ngZU'></th></span></q></dt></tr></i><div id='3ngZU'><tfoot id='3ngZU'></tfoot><dl id='3ngZU'><fieldset id='3ngZU'></fieldset></dl></div>
                  <tfoot id='3ngZU'></tfoot>

                      <small id='3ngZU'></small><noframes id='3ngZU'>

                        <bdo id='3ngZU'></bdo><ul id='3ngZU'></ul>