2012-03-27 59 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