在java中从对象表检索结构类型
您好,我需要从oracle对象表(address)获取值,为此,我想将java类(address)映射为oracle类型(address)。从Jframe中的一个按钮,我可以看到打印al数据的内容。因此: Sql:在java中从对象表检索结构类型,java,oracle,plsql,oracle11g,Java,Oracle,Plsql,Oracle11g,您好,我需要从oracle对象表(address)获取值,为此,我想将java类(address)映射为oracle类型(address)。从Jframe中的一个按钮,我可以看到打印al数据的内容。因此: Sql: 创建或替换 键入地址作为对象( 无varchar2(7), str varchar2(40), 瓦查尔市2(25), 瓦查尔国家2(25) ); create table airp (id_pk number(5), location address_t); Java地址类: im
创建或替换
键入地址作为对象(
无varchar2(7),
str varchar2(40),
瓦查尔市2(25),
瓦查尔国家2(25)
);代码>
create table airp (id_pk number(5),
location address_t);
Java地址类:
import test.Test;
import java.sql.Connection;
import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Address implements SQLData {
private String str;
private String no;
private String city;
private String country;
private String sql_type;
private Connection conn = null;
public Address(){
}
public Address(String sql_type, String no, String str, String city , String country){
this.sql_type = sql_type;
this.no = no;
this.str = str;
this.city = city;
this.country = country;
}
@Override
public String getSQLTypeName() throws SQLException {
return this.sql_type;
}
@Override
public void readSQL(SQLInput stream, String typeName) throws SQLException {
this.sql_type = typeName;
no = stream.readString();
str = stream.readString();
city = stream.readString();
country = stream.readString();
}
@Override
public void writeSQL(SQLOutput stream) throws SQLException {
stream.writeString(no);
stream.writeString(str);
stream.writeString(city);
stream.writeString(country);
}
@Override
public String toString(){
return sql_type+" "+no+" "+" "+city+" "+country;
}
}
和按钮:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
java.util.Map map = con.getTypeMap();
map.put("LI.ADDRESS_T", Class.forName("test.Address"));
con.setTypeMap((Map)map);
map = con.getTypeMap();
sql = "select * from aerop";
st = (OracleStatement)con.createStatement();
rs = (OracleResultSet)st.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getObject(1));
}
} catch (SQLException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}}
我得到下一个错误
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at test.Test.jButton1ActionPerformed
con
或con.getTypeMap()
为null
。你为什么不调试它?什么?你是在讽刺还是真的这么想?:)我已经做了差不多两个小时了,因为我已经声明了连接,但是我忘记了创建它,新手错了