Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Cara mendapatkan tabel sebagai parameter keluar di Oracle

Anda harus mendefinisikan objek sqldata untuk memetakan ini.

dokumentasi:http://docs.Oracle.com /cd/E11882_01/java.112/e16548/oraarr.htm#JJDBC28574

misalnya:

SQL> create or replace TYPE tableOneExample AS OBJECT (
  2        somethingOne                 VARCHAR2 (4)
  3       ,somethingTwo        NUMBER (12)
  4  );
  5  /

Type created.

SQL> create or replace TYPE outputOneSQLType IS TABLE OF tableOneExample;
  2  /

Type created.

SQL>
SQL> create or replace PROCEDURE myprocedure (
  2  inputParam                IN       VARCHAR2,
  3  outputOne                 OUT outputOneSQLType)
  4  as
  5  begin
  6  outputOne  := outputOneSQLType(tableOneExample('a', 1), tableOneExample('b', 2));
  7  end;
  8  /

Procedure created.

sekarang kita mendefinisikan antarmuka SQLDATA:

import java.sql.*;

public class TestArr implements SQLData
 {
  private String sql_type;

  public String attrOne;
  public int    attrTwo;

  public TestArr() 
  { 
  }
  public TestArr (String sql_type, String attrOne, int attrTwo)
  {
    this.sql_type = sql_type;
    this.attrOne = attrOne;
    this.attrTwo = attrTwo;
   }

  // define a get method to return the SQL type of the object
  public String getSQLTypeName() throws SQLException
  { 
    return sql_type; 
  } 

  // define the required readSQL() method 
  public void readSQL(SQLInput stream, String typeName)
    throws SQLException
  {
    sql_type = typeName;

    attrOne = stream.readString();
    attrTwo = stream.readInt();
  }  
  // define the required writeSQL() method 
  public void writeSQL(SQLOutput stream)
    throws SQLException
  { 
    stream.writeString(attrOne);
    stream.writeInt(attrTwo);
  }
}

pastikan input dan urutan penulisan/pembacaan aliran sama dengan tipe oracle Anda, karena inkonsistensi apa pun akan menghasilkan kesalahan representasi internal.

kemudian di kelas utama, Anda akan memetakan ini sebagai berikut:

CallableStatement stmt = conn.prepareCall("begin myprocedure(?,?); end;");
stmt.setString(1, "foo");
stmt.registerOutParameter(2, java.sql.Types.ARRAY, "OUTPUTONESQLTYPE"); // YOUR ARRAY TYPE (TO MATCH THE API OUTPUT), NOT OBJECT
stmt.execute();
Array arr = stmt.getArray (2);
Map map = conn.getTypeMap();
map.put("TABLEONEEXAMPLE", Class.forName("TestArr")); // YOUR OBJECT TYPE, NOT ARRAY.
Object[] values = (Object[]) arr.getArray();
for (int i=0; i < values.length; i++)
{
  TestArr a = (TestArr)values[i];
  System.out.println("somethingOne: " + a.attrOne);
  System.out.println("somethingTwo: " + a.attrTwo);
}

hasilnya bieng:

M:\Documents\Sample Code\1>javac TestArr.java

M:\Documents\Sample Code\1>javac ArrayTest.java
Note: ArrayTest.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

M:\Documents\Sample Code\SQLComplexArray>java ArrayTest
Opening Oracle connection...done.
somethingOne: a
somethingTwo: 1
somethingOne: b
somethingTwo: 2



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Saring catatan yang tidak ada dalam format tanggal oracle ini

  2. Koneksi string agregat di Oracle 10g

  3. Eksekusi kueri Oracle Ad Hoc dengan Parameter Bind Erroring Out; ORA-00907:tanda kurung kanan hilang

  4. Cara Mengembalikan Detik Pecahan dari Nilai Datetime di Oracle

  5. SQL (Oracle) untuk membandingkan dua daftar