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

问题描述

我正在学习如何使用SQLData并在回送到我的对象时遇到问题。

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

My Oracle Types看起来像这样:

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?

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

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();

谢谢!
Steve

Thanks!Steve

(大部分上面是内存,因为代码在工作...)

(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 。完成后,以下内容应足以阅读活动中的活动列表

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 - 使用列表/数组转换为用户对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 23:31