介绍
我正在创建一个从oraclesql数据库上的Map对象读取的接口。到目前为止还不错,我成功地创建了类型和表,但是当用 java.sql.SQLData
它给了我这个铸造错误。
代码
public class EmpresaSqlData extends Empresa implements SQLData {
private String sql_type = "USER.EMPRESA";
public EmpresaSqlData() {}
public EmpresaSqlData(String cif, String nom, Contacte contacte) {
super(cif, nom, contacte);
}
public EmpresaSqlData(String cif, String nom) {
super(cif, nom);
}
@Override
public String getSQLTypeName() throws SQLException {
return sql_type;
}
@Override
public void readSQL(SQLInput stream, String typeName) throws SQLException {
sql_type = typeName;
setNom(stream.readString());
setCif(stream.readNString());//Write the object
setContacte(stream.readObject(new Contacte)); //Fix??
}
@Override
public void writeSQL(SQLOutput stream) throws SQLException {
stream.writeString(getNom());
stream.writeString(getCif());
stream.writeObject(new ContacteSqlData());
}
}
当我执行以下查询时,需要进行上述操作:
@Override
public List<EmpresaSqlData> findAll() {
EmpresaSqlData empresaSqlDataSQLData;
List<EmpresaSqlData> corpList = new ArrayList<>();
String query = "SELECT VALUE (e) FROM empreses e";
try (Connection conn = cf.getConnection();
PreparedStatement statement
= conn.prepareStatement(query);){
//Initial connection setup
conn.setTypeMap(map);
ResultSet rs = statement.executeQuery();
while (rs.next()){
System.out.println(rs.getObject(1, EmpresaSqlData.class)); //Error here "Type of column not valid"
}
return corpList;
} catch (SQLException e) {
Logger.getLogger(EmpresaDAOImpl.class.getName()).log(Level.SEVERE, null, e);
return null;
}
}
研究
从“implementing readsql()and writesql()methods”(ctrl+c和ctrl+f+enter)的文档中可以看出:
· The readSQL() method takes as input a SQLInput stream and a string that indicates the SQL type name of the data (in other words, the name of the Oracle object type, such as EMPLOYEE)
问题
然后我尝试读取 readSQL()
并将其转换为java对象 Contacte
```
setContacte((Contacte)stream.readObject());
这给了我一个例外:
Exception in thread "main" java.lang.ClassCastException: oracle.sql.STRUCT cannot be cast to activitat3.logica.Contacte
at activitat3.logica.EmpresaSqlData.readSQL(EmpresaSqlData.java:44)
at oracle.sql.STRUCT.toClass(STRUCT.java:662)
at oracle.sql.STRUCT.toJdbc(STRUCT.java:599)
at oracle.jdbc.driver.NamedTypeAccessor.getObject(NamedTypeAccessor.java:140)
at oracle.jdbc.driver.NamedTypeAccessor.getObject(NamedTypeAccessor.java:110)
at oracle.jdbc.driver.GeneratedStatement.getObject(GeneratedStatement.java:261)
at oracle.jdbc.driver.GeneratedScrollableResultSet.getObject(GeneratedScrollableResultSet.java:761)
at activitat3.dades.impl.EmpresaDAOImpl.findAll(EmpresaDAOImpl.java:184)
at activitat3.test.Main.llistarEmpreses(Main.java:330)
at activitat3.test.Main.main(Main.java:93)
此异常表示使用 `readSQL()` 方法来自 `java.sql.SQLData` 不能强制转换为java对象。
我怎么能用这种方式处理一个物体?
readint()返回整数,readstring返回字符串,但readobject()不能被强制转换或给定一个自定义对象?
目标
我想用这种方法从oraclesql数据库中读取Map对象。
1条答案
按热度按时间xj3cbfub1#
从评论来看,问题似乎是
Contacte
类丢失或不完整。stacktrace显示
EmpresaSqlData
类,因此此类型的Map正在工作。如果Contacte
类没有从数据库Map,那么可能没有这样的oracle类型Map,或者直接Map到这个类,或者,按照您的建议EmpresaSqlData
,它的一个子类。