2012-03-27 132 views
0

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

事情是我使用getInt()的数据再次是原始数据。我想获取用原始数据取代的数据。我不打算提交对数据库的更改。

我以为我可以使用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; 
    } 
} 

回答

0

的解决方法是调用cachedRowSet.updateRow()使用适当的更新方法(updateInt(),updateString()等),使写入存储器的变化之后。我之前没有使用它,因为此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 

     } 
    } 
0

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

oracleCachedRowSet.setReadOnly(false); 

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

编辑:基于您发布的代码,

你是正确的观察,你正在做一个按值传递。事实上,在java中,它始终传递值,永远不会传递参考。

解决方案:

现在你的函数返回无效,将其更改为返回更新cachedRowSet。你的函数定义看起来像

public CachedRowSet updateRowSetData(CachedRowSet cachedRowSet, int columnIndex, int columnType, Object data) 
     throws SQLException 
+0

我试过了,但遗憾的是它没有工作 – Bren 2012-03-27 10:37:26

+0

可以发布你正在处理的一些示例代码? – Santosh 2012-03-27 11:08:39

+0

当然,相应地编辑我的问题@Santosh – Bren 2012-03-27 11:27:47