我有一段代码,其中我将数组列表转换为字节数组,然后将该字节数组保存为MySQL数据库中的BLOB。以下是代码:
Object temp = attributes.get(columnName);
if (temp instanceof List && temp != null) {
List extraAttributes = (ArrayList) temp;
resultStmt.setBytes(currentIndex, createByteArray(extraAttributes));
createByteArray
方法的定义如下:
private byte [] createByteArray( Object obj)
{
byte [] bArray = null;
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream objOstream = new ObjectOutputStream(baos);
objOstream.writeObject(obj);
bArray = baos.toByteArray();
}
catch (Exception e)
{
TraceDbLog.writeError("Problem in createByteArray", e);
}
return bArray;
}
以上代码早先是用于将 HashMap 写入 BLOB 的,现在我也将其用于将包含 HashMap 的 ArrayList 转换为 BLOB。
读取 BLOB 时遇到的问题。
private Object readBytes (ResultSet rs, String columnName)
throws SQLException
{
ObjectInputStream ois = null;
byte [] newArray;
Object obj = null;
try
{
newArray = rs.getBytes(columnName);
ois = new ObjectInputStream (new ByteArrayInputStream(newArray));
obj = ois.readObject ();
}
在读取部分,对象并不是作为HashMap数组的ArrayList出现,并且在Eclipse的调试视图中也无法检查到该对象。
我已经尝试将对象强制转换为List,但仍然无法获得正确的响应。 请告诉我在读写上述BLOB时是否存在任何缺陷。