Beberapa perubahan kecil dari kode Anda - menggunakan oracle.sql.ARRAY
daripada java.sql.Array
dan kemudian menggunakan OracleConnection.setARRAYAtName()
daripada Connection.setObject()
untuk mengikat parameter.
(Catatan:ini bekerja menggunakan Oracle Driver ojdbc6.jar dengan Oracle 11.2.0.1)
Pengaturan Oracle;
CREATE TYPE rec_type AS OBJECT( id NUMBER, descr VARCHAR2(100) );
/
CREATE TYPE tab_type AS TABLE OF rec_type;
/
CREATE TABLE bom OF rec_type;
/
CREATE PROCEDURE pBom( t IN tab_type )
IS
BEGIN
FORALL i IN INDICES OF t
INSERT INTO bom VALUES t(i);
END;
/
Jawa :
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleCallableStatement;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
public class TestDatabase2 {
public static void main(String args[]){
Connection con = null;
try{
Class.forName("oracle.jdbc.OracleDriver");
con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:XE",
"username",
"password"
);
ArrayDescriptor des = ArrayDescriptor.createDescriptor("TAB_TYPE", con);
Object[] o1 = { 1, "ABC" };
Object[] o2 = { 3, "DEF" };
Object[] o3 = { 2, "GHI" };
ARRAY objs = new ARRAY( des, con, new Object[][]{ o1, o2, o3 } );
CallableStatement st = con.prepareCall("{ call pBOM( :arr )}");
((OracleCallableStatement) st).setARRAYAtName( "arr", objs );
st.execute();
} catch(ClassNotFoundException | SQLException e) {
System.out.println(e);
} finally {
try {
if ( con != null )
{
con.close();
}
} catch ( SQLException e ){}
}
}
}