Jdbc之写入Oracle自定义嵌套类型(Nested Table Type)
简介
Oracle Database supports user-defined data types that make it possible to model real-world entities as objects in the database. Collection types are object data types for modeling multi-valued attributes. Nested tables are collection types. Nested tables can be used anywhere that other data types can be used.
表结构准备
CREATE OR REPLACE TYPE obj1 AS OBJECT(m1 int,m2 varchar2(20));
CREATE OR REPLACE TYPE obj2 AS OBJECT(p1 int,p2 ojb1);
CREATE TABLE test(id int,name varchar2(20),c1 obj2);
JDBC语句
PreparedStatement preparedStatement = connection.prepareStatement("insert into test values (?,?,?)");
Struct obj1 = connection.createStruct("obj1", new Object[]{1, "string"});
Struct obj2 = connection.createStruct("obj2", new Object[]{2, obj1});
preparedStatement.setInt(1, 1);
preparedStatement.setString(2, "string");
preparedStatement.setObject(3, obj2, Types.STRUCT);
preparedStatement.execute();