0

我正在尝试做本教程: http ://docs.oracle.com/javase/tutorial/jdbc/basics/sqlcustommapping.html 到目前为止,我已经在 oracle db 中创建了“地址”类型,并在其中创建了一个表“地址”类型的列,我已经用教程中的数据填充了一条记录。在java中,我创建了类地址(从链接复制),我有地图,可调用语句与链接中的示例完全相同。当我执行我的程序时,这是我得到的异常: java.lang.ClassCastException: oracle.sql.STRUCT

我的代码的一部分:

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();
    }

整个堆栈跟踪:

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

此致!

@新白痴:

这是我的地址类:

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);
    }
}

这是我在 oracle 中的类型:

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

@Smit:如果 mypackage 从中删除,下面是我得到的异常

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

1 回答 1

1

这个谜团的答案是

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

一定是这个

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

如果有人想进一步解释:这里

于 2013-07-05T07:59:38.013 回答