Question

I am trying to do this tutorial: http://docs.oracle.com/javase/tutorial/jdbc/basics/sqlcustommapping.html and so far I have created the 'ADDRESS' type in oracle db, and a table where there is column of type 'ADDRESS', i have populated the tabale with one single record with the data from the tutorial. In java i have created the class address(copied from the link), i have the map, the callable statement exactly like the example in the link. When i am executing my program this is the exeption that i get: java.lang.ClassCastException: oracle.sql.STRUCT

part of my code:

Map map = conn.getTypeMap();        
    try {
        map.put("my_schema.ADDRESS", Class.forName("mypackage.Address"));
        conn.setTypeMap(map);

        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT ADDRESS FROM my_schema.ADDRESS_TABLE");
            while (rs.next()) {

                Address location = (Address)rs.getObject("ADDRESS");

                System.out.println(location.city);
            }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

the whole stacktrace:

java.lang.ClassCastException: oracle.sql.STRUCT
at mypackage.Example.test(Example.java:89)
at mypackage.Example.main(Example.java:52)

Best Regards!

@The New Idiot:

This is my address class:

public class Address implements SQLData {
    public String num;
    public String street;
    public String city;
    public String state;
    public String zip;
    private String sql_type;

    public String getSQLTypeName() {
        return sql_type;
    }

    public void readSQL(SQLInput stream, String type)
        throws SQLException {
        sql_type = type;
        num = stream.readString();
        street = stream.readString();
        city = stream.readString();
        state = stream.readString();
        zip = stream.readString();
    }

    public void writeSQL(SQLOutput stream)
        throws SQLException {
        stream.writeString(num);
        stream.writeString(street);
        stream.writeString(city);
        stream.writeString(state);
        stream.writeString(zip);
    }
}

And this is my type in oracle:

CREATE OR REPLACE TYPE "ADDRESS" AS OBJECT 
(NUM       VARCHAR2(60),
 STREET    VARCHAR2(60),
 CITY      VARCHAR2(60),
 STATE     VARCHAR2(60),
 ZIP       VARCHAR2(60));

@Smit: Down below is the exception I get if mypackage is removed from

map.put("my_schema.ADDRESS", Class.forName("mypackage.Address"));

    java.lang.ClassNotFoundException: Address
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:164)
at mypackage.Example.test(Example.java:77)
at mypackage.Example.main(Example.java:52)
Was it helpful?

Solution

The answer to the mystery is that this

  map.put("my_schema.ADDRESS", Class.forName("mypackage.Address"));

must be this

  map.put("MY_SCHEMA.ADDRESS", Class.forName("mypackage.Address"));

For further explanation if someone wonder: Here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top