本文介绍了Java SQLData - 使用列表/数组转换为用户对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用 SQLData 并在转换回我的对象​​时遇到问题.

I'm learning on how to use SQLData and having an issue with casting back to my object.

我的 Oracle 类型如下所示:

My Oracle Types looks something like this:

CREATE OR REPLACE TYPE activities_t AS OBJECT
(
   list   activity_list_t;
);

CREATE OR REPLACE TYPE activity_list_t AS TABLE OF activity_t;

CREATE OR REPLACE TYPE activity_t AS OBJECT
(
   startDate  DATE;
   endDate    DATE;
);

我的 Java 看起来像这样:

And my Java looks like this:

public class Activities implements SQLData {
    private String sqlType = "ACTIVITIES_T";
    List<Activity> list;

    // must have default ctor!
    public Activities() {
    }

    public String getSQLTypeName() throws SQLException
    {
        return sqlType;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public void readSQL(SQLInput stream, String typeName) throws SQLException
    {
        Array a = stream.readArray();
        // :(
    }

    public void writeSQL(SQLOutput stream) throws SQLException
    {
        // stream.writeArray(this.list);
    }
}

我在 readSQL 中尝试了一些东西,但没有取得多大成功 - 我错过了什么?

I've tried a few things in readSQL but I am not having much success - what am I missing?

我正在使用 JDBC 调用一个 PLSQL 存储过程,它有一个activities_t"的 OUT 参数:

I am calling a PLSQL stored procedure which has an OUT parameter of "activities_t" using JDBC:

Map map = connection.getTypeMap();
map.put("ACTIVITIES_T", Class.forName("Activities"));
connection.setTypeMap(map);

callableStatement = connection.prepareCall("{call GET_ACTIVITIES(?)}");
callableStatement.execute();

谢谢!史蒂夫

(以上大部分内容来自内存,因为代码正在运行......)

(most of the above is from memory as the code is at work...)

推荐答案

您需要为 ACTIVITY_T 类型和 ACTIVITIES_T 类型添加类型映射>.从您的问题中不清楚您是否已经这样做了.

You'll need to add a type mapping for the type ACTIVITY_T as well as the one for ACTIVITIES_T. It's not clear from your question whether you've already done this.

假设您已完成此操作并创建了一个名为 Activity 的类,该类也实现了 SQLData.完成此操作后,以下内容应该足以阅读 Activities 中的活动列表:

Let's assume you've done this and created a class called Activity which implements SQLData as well. Once you've done that, the following should suffice to read the activity list within Activities:

    public void readSQL(SQLInput stream, String typeName) throws SQLException {
        Array array = stream.readArray();
        this.list = new ArrayList<Activity>();
        for (Object obj : (Object[])array.getArray()) {
            list.add((Activity)obj);
        }
    }

这篇关于Java SQLData - 使用列表/数组转换为用户对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-15 18:39