0

大家好,我需要从 oracle 对象表(地址)中获取值,为此我想用 oracle 类型(地址)映射一个 java 类(地址)。从 Jframe 中的按钮我可以打印 al 数据。所以 :

数据:

create or replace type address_t as object( no varchar2(7), str varchar2(40), city varchar2(25), country varchar2(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
4

1 回答 1

0

con或者con.getTypeMap()null。你为什么不调试它?

于 2013-06-24T14:01:08.720 回答