我正在使用 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;
}
}