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

Memasukkan ke Oracle Nested Table di Java

Lewati larik Java sebagai koleksi:

Penyiapan Oracle 12c :

CREATE USER test_user IDENTIFIED BY password;
GRANT CREATE SESSION TO test_user;
ALTER USER test_user QUOTA UNLIMITED ON users;

CREATE TYPE test_user.nested_row_type AS OBJECT( a CHAR(1), b INTEGER );
/

CREATE TYPE test_user.nested_tbl_type AS TABLE OF test_user.nested_row_type;
/

CREATE TABLE test_user.container_tbl(
  a CHAR(1),
  b CHAR(1),
  nested_tbl test_user.nested_tbl_type
) NESTED TABLE nested_tbl STORE AS nested_tbl_tbl;

Jawa :(Menggunakan ojdbc7.jar )

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OraclePreparedStatement;
import oracle.sql.ARRAY;

public class LoadOracleObjectCollection {
  public static void main(String[] args) {
    try{
      Class.forName("oracle.jdbc.OracleDriver");

      Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","test_user","password");

      Object[] objs = new Object[]{
        con.createStruct( "NESTED_ROW_TYPE", new Object[]{ "Q", 99 } ),
        con.createStruct( "NESTED_ROW_TYPE", new Object[]{ "R", 999 } )
      };

      ARRAY a = ((OracleConnection) con).createARRAY("NESTED_TBL_TYPE", objs);

      PreparedStatement st = con.prepareCall( "INSERT INTO container_tbl ( a, b, nested_tbl ) VALUES ( ?, ?, ? )" );

      st.setString( 1, "x" );
      st.setString( 2, "y" );
      ((OraclePreparedStatement) st).setARRAY( 3 , a );
      st.execute();
      st.close();
      con.close();
    } catch(ClassNotFoundException | SQLException e) {
      System.out.println(e);
    }
  }
}

Kueri Oracle

SELECT c.a, c.b, n.a, n.b
FROM   test_user.container_tbl c
       CROSS JOIN TABLE( c.nested_tbl ) n;

Hasil :

A B A          B
- - - ----------
x y Q         99
x y R        999

Versi dengan sintaks lama :

Hanya meneruskan ke dan dari kueri dummy (daripada memasukkan ke dalam database) untuk juga menunjukkan cara mengambil larik objek:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OraclePreparedStatement;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import oracle.sql.Datum;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;

public class ArrayOfObjectsTest
{
  public static void main( final String[] args ){
    try{
      Class.forName( "oracle.jdbc.OracleDriver" );

      Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","test_user","password");

      OracleConnection oc = (OracleConnection) con;
      StructDescriptor sd = new StructDescriptor( "NESTED_ROW_TYPE", oc );
      ArrayDescriptor  ad = new ArrayDescriptor( "NESTED_TBL_TYPE", oc );

      ARRAY array = new ARRAY( ad,oc,new STRUCT[]{
        new STRUCT(sd,oc,new Object[]{ 'P',99 } ),
        new STRUCT(sd,oc,new Object[]{ 'Q',999 } )
      } );

      OraclePreparedStatement st = (OraclePreparedStatement) con.prepareStatement( "SELECT ? FROM DUAL" );
      st.setARRAY( 1, array);
      ResultSet rs = st.executeQuery();

      while( rs.next() )
      {
        Object[] structs = (Object[]) rs.getArray( 1 ).getArray();
        for ( Object struct : structs )
        {
          Datum[] datums = ((STRUCT) struct).getOracleAttributes();
          System.out.println( datums[0].stringValue() + ", " + datums[1].intValue() ) );
        }
      }
      st.close();
      con.close();
    } catch (ClassNotFoundException | SQLException ex) {
      System.out.println( ex.getMessage() );
      ex.printStackTrace();
    }
  }  
}

Keluaran :

P, 99
Q, 999

Ini dikompilasi untuk saya dengan ojdbc6.jar dan bekerja dengan Oracle 11gR2. Anda harus menemukan ojdbc yang benar versi untuk database Anda dan gunakan itu.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Memotong tabel dalam prosedur tersimpan

  2. Oracle 12c Fitur Baru Teratas

  3. Pengaturan NLS_NUMERIC_CHARACTERS untuk desimal

  4. 6 Cara Menghapus Baris Duplikat yang memiliki Kunci Utama di Oracle

  5. Bagaimana cara melihat Explain Plan di pengembang Oracle Sql?