0

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)
4
  • Can you post the code of the Address class ? And how is the ADDRESS datatype defined in DB ! Commented Jul 2, 2013 at 15:41
  • I think your Class.forName("mypackage.Address") should be Class.forName("Address") only. From your stactrace, its not recognizing the returned element type as Address but as a Struct Commented Jul 2, 2013 at 16:18
  • @TheNewIdiot I have added them in the question. Commented Jul 3, 2013 at 6:18
  • @Smit in the question I have added what happens if I remove mypackage from: map.put("my_schema.ADDRESS", Class.forName("mypackage.Address")); Commented Jul 3, 2013 at 6:25

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.