从java中的对象表中检索结构类型

Posted

技术标签:

【中文标题】从java中的对象表中检索结构类型【英文标题】:Retrieve struct type from a object table in 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

【问题讨论】:

【参考方案1】:

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

【讨论】:

什么?你是在讽刺还是你真的是认真的? :) 我已经在那个东西上工作了将近 2 个小时,因为我已经声明了连接但我忘了创建它,新手错误

以上是关于从java中的对象表中检索结构类型的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server阻止保存修改表结构的解决方法

使用 Java Stream api 从对象层次结构中提取数据 [关闭]

C语言数据结构——查找(检索)

JAVA数据结构之集合

JAVA数据结构之集合

java 比对2个相同结构表中的差异数据