我正在尝试这个教程: http://docs.oracle.com/javase/tutorial/jdbc/basics/sqlcustommapping.html 到目前为止,我已经创建了地址'在oracle db中键入一个表,其中有一个类型为' ADDRESS'的列,我已经用一个记录填充了一个包含教程数据的tabale。在java中我创建了类地址(从链接复制),我有了map,可调用语句与链接中的示例完全一样。当我执行我的程序时,这是我得到的例外: 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();
}
整个stacktrace:
java.lang.ClassCastException: oracle.sql.STRUCT
at mypackage.Example.test(Example.java:89)
at mypackage.Example.main(Example.java:52)
最诚挚的问候!
@The New Idiot:
这是我的地址类:
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)
答案 0 :(得分:1)
这个谜的答案是这个
map.put("my_schema.ADDRESS", Class.forName("mypackage.Address"));
一定是这个
map.put("MY_SCHEMA.ADDRESS", Class.forName("mypackage.Address"));
有关进一步解释,如果有人想知道:Here