Brug java.sql.ResultSetMetaData.getColumnTypeName()
:
Oracle-opsætning :
CREATE OR REPLACE TYPE random_array IS TABLE OF VARCHAR2(42);
/
Java :
Class.forName("oracle.jdbc.OracleDriver");
Connection con = DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl",
"username",
"password"
);
PreparedStatement st = con.prepareStatement(
"SELECT random_array( 'a', 'b', 'c' ) AS array FROM DUAL"
);
ResultSet rs = st.executeQuery();
while( rs.next() )
{
String type = rs.getMetaData().getColumnTypeName(1);
String[] data = (String[]) rs.getArray(1).getArray();
System.out.println( type + ": " + Arrays.toString( data ) );
}
st.close();
con.close();
Output :
SCHEMA_NAME.RANDOM_ARRAY: [a, b, c]
Hvis du så vil gå tilbage til Oracle og finde typen af samlingselementerne kan du bruge:
SELECT ELEM_TYPE_NAME,
LENGTH,
PRECISION,
SCALE
FROM ALL_COLL_TYPES
WHERE TYPE_NAME = 'RANDOM_ARRAY'
AND OWNER = 'SCHEMA_NAME';
Hvilken udgang:
ELEM_TYPE_NAME LENGTH PRECISION SCALE
-------------- ------ --------- -----
VARCHAR2 42