0

我正在使用 oracle jdbc cachedrowset 实现来选择从查询返回的几行。然后我使用 cachedrowset.updateInt() 或其他更新方法更新一些数据。我首先使用 cachedrowset.beforeFirst() 取回光标,然后再次遍历行集以打印数据。

问题是我再次使用 getInt() 获得的数据是原始数据。我想获得被原始数据替换的数据。我不打算对 db 进行更改。

我想我可以使用 Rowset 对象作为数据包装器而不更改数据库上的任何数据,仅用于数据操作和查看。有什么办法可以让我得到更新的日期而不是原来的日期?我不想编写自己的数据包装器对象

编辑:这是我获取数据的方式,下面是我更新它的方式

public OracleCachedRowSet getCachedRowset( String query, Connection con)
        throws DTSException {
    try {
        OracleCachedRowSet cachedRowSet = new OracleCachedRowSet();
        cachedRowSet.setReadOnly(false);
        cachedRowSet.setCommand(query);
        cachedRowSet.execute(con);
        return cachedRowSet;
    } catch (SQLException sqle) {
        throw new DTSException("Error fetching data! :" + sqle.getMessage(), sqle);
    }
}

更新代码:

public void updateRowSetData(CachedRowSet cachedRowSet, int columnIndex, int columnType,    Object data)
        throws SQLException {

    switch (columnType) {
    case Types.NUMERIC:
    case Types.DECIMAL:
        cachedRowSet.updateBigDecimal(columnIndex, (BigDecimal) data);
        return;
    case Types.CHAR:
    case Types.VARCHAR:
    case Types.LONGNVARCHAR:
        cachedRowSet.updateString(columnIndex, data == null ? null : data.toString());
        return;
    case Types.INTEGER:
        cachedRowSet.updateInt(columnIndex, (Integer) data);
        return;
    case Types.DATE:
        cachedRowSet.updateDate(columnIndex, (Date) data);
        return;
    case Types.TIMESTAMP:
        cachedRowSet.updateTimestamp(columnIndex, (Timestamp) data);
        return;
    case Types.TIME:
        cachedRowSet.updateTime(columnIndex, (Time) data);
        return;
    case Types.BIGINT:
        cachedRowSet.updateLong(columnIndex, data == null ? null : Long.parseLong(data.toString()));
        return;
    case Types.DOUBLE:
    case Types.FLOAT:
        cachedRowSet.updateDouble(columnIndex, (Double) data);
        return;
    case Types.SMALLINT:
        cachedRowSet.updateShort(columnIndex, data == null ? null : Short.parseShort(data.toString()));
        return;
    case Types.TINYINT:
        cachedRowSet.updateByte(columnIndex, Byte.parseByte(data == null ? null : data.toString()));
        return;
    case Types.BINARY:
    case Types.VARBINARY:
        cachedRowSet.updateBytes(columnIndex, (byte[]) data);
        return;
    case Types.CLOB:
        if (data != null) {
            cachedRowSet.updateClob(columnIndex, ((Clob) data).getCharacterStream());
        } else {
            cachedRowSet.updateClob(columnIndex, (Clob) data);
        }
        return;
    case Types.ARRAY:
        cachedRowSet.updateArray(columnIndex, (Array) data);
        return;
    case Types.BLOB:
        if (data != null) {
            cachedRowSet.updateBlob(columnIndex, data == null ? null : ((Blob) data).getBinaryStream());
        } else {
            cachedRowSet.updateBlob(columnIndex, (Blob) data);
        }
        return;
    case Types.REAL:
        cachedRowSet.updateFloat(columnIndex, (Float) data);
        return;
    case Types.BIT:
    case Types.BOOLEAN:
        cachedRowSet.updateBoolean(columnIndex, (Boolean) data);
        return;
    case Types.REF:
        cachedRowSet.updateRef(columnIndex, (Ref) data);
        return;
    case Types.LONGVARBINARY:
        cachedRowSet.updateBinaryStream(columnIndex, (InputStream) data);
        return;
    default:
        cachedRowSet.updateObject(columnIndex, data);
        return;
    }
}
4

2 回答 2

0

请尝试如下更改 OracleCachedRowSet 的只读设置。

oracleCachedRowSet.setReadOnly(false);

此方法在行集类实现的javax.sql.RowSet中定义。

编辑:根据您发布的代码,

您观察到您正在通过价值传递是正确的。事实上,在java中它总是按值传递而不是按引用传递。

解决方案:

现在您的函数正在返回 void,更改它以返回更新后的cachedRowSet. 您的函数定义将如下所示

public CachedRowSet updateRowSetData(CachedRowSet cachedRowSet, int columnIndex, int columnType,    Object data)
        throws SQLException
于 2012-03-27T09:34:16.863 回答
0

解决方案是在使用适当的更新方法(updateInt()、updateString() 等)将更改写入内存后调用 cachedRowSet.updateRow()。我之前没有使用它,因为这个 upateRow() 的 JavaDoc 说:“用这个 ResultSet 对象的当前行的新内容更新底层数据库。”

只是这不是发生的事情。updateRow() 更新内存上的数据,而不是底层数据库上的数据。我在链接的文档中找到了解决方案:http ://www.scribd.com/doc/68052701/8/Setting-Up-a-CachedRowSet-Object

所以我所做的只是在更新数据后调用 updateRow :

    while (cachedRowSet.next()) {
        for (int i = 0; i < columnCount; i++) {

                dataHandler.updateRowSetData(cachedRowSet, i + 1, columnTypes[i], getUpdatedData());
                cachedRowSet.updateRow(); //Adding this line solves the problem. Otherwise changes are not made

        }
    }
于 2012-03-28T08:02:35.100 回答