Tuesday, 21 August 2007

SYS.ANYDATA Oracle Type support in 11g JDBC Driver

This release of Oracle JDBC driver provides a Java interface to access SYS.ANYTYPE and SYS.ANYDATA Oracle types. His a small piece of code showing how to access a SYS.ANYDATA column from JDBC 11g.

Table definition:

create table anydata_table
(col1 number,
col2 sys.anydata)
/

Java Code snippet:

stmt = conn.createStatement();
rset = stmt.executeQuery("select * from anydata_table");
int i = 0;
while (rset.next())
{
i++;
System.out.println("** Row " + i);
ANYDATA anydata = (ANYDATA)rset.getObject(2);
Datum embeddedDatum = anydata.accessDatum();
TypeDescriptor typeDescriptor = anydata.getTypeDescriptor();
int typeCode = typeDescriptor.getTypeCode();
if (typeCode == TypeDescriptor.TYPECODE_TIMESTAMP)
{
// the embedded object is a DATE
TIMESTAMP datedatum = (TIMESTAMP)embeddedDatum;
System.out.println("Timestamp Type");
System.out.println(datedatum.stringValue());
}
else if (typeCode == TypeDescriptor.TYPECODE_NUMBER)
{
// the embedded object is a NUMBER
NUMBER numberdatum = (NUMBER)embeddedDatum;
System.out.println("Number Type");
System.out.println(numberdatum.stringValue());
}
else if (typeCode == TypeDescriptor.TYPECODE_VARCHAR2)
{
// the embedded object is a VARCHAR2
String varchardatum = embeddedDatum.stringValue();
System.out.println("VARCHAR2 Type");
System.out.println(varchardatum);
}
}

More information on this can be found in the documentation

http://download.oracle.com/docs/cd/B28359_01/java.111/b31224/oraint.htm#CHDEBICE

No comments: