【问题标题】:Retrieve struct type from a object table in java从java中的对象表中检索结构类型
【发布时间】:2013-06-24 13:59:12
【问题描述】:

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

Sql:

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

【问题讨论】:

    标签: java oracle plsql oracle11g


    【解决方案1】:

    concon.getTypeMap()null。为什么不调试呢?

    【讨论】:

    • 什么?你是在讽刺还是你真的是认真的? :)
    • 我已经在那个东西上工作了将近 2 个小时,因为我已经声明了连接但我忘了创建它,新手错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多