I have a code where i am writing a list of hashMap into database column where column type is BLOB. Earlier the code was written to insert hash map into database which i am modifying to insert List of Hash Map.
Read and write code is as follows:-
WRITE:-
Object temp = attributes.get(columnName);
if (temp instanceof List && temp != null) {
List extraAttributes = (ArrayList) temp;
resultStmt.setBytes(currentIndex, createByteArray(extraAttributes));
}
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;
}
READ :- Here TableType.Map is not java.util.Map type its a declaration for defning column type in database schema.
else if (columnType.equals(TableType.MAP))
{
if (resultSet.getBytes(columnName) != null)
{
resultMap.put(columnName, readBytes(resultSet, columnName));
}
}
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 ();
}
catch (Exception e)
{
throw new SQLException (getClass() + ":readBytes: " + e);
}
finally
{
if (ois != null)
{
try
{
ois.close ();
}
catch (IOException e)
{
throw new SQLException (getClass() + ":readBytes: " + e);
}
}
}
return obj;
}
PROBLEM
Write has no issues associated. When i am going for a read operation then
obj = ois.readObject ();
In this line obj is coming as: com.sun.jdi.InvocationException occurred invoking method.
Thus there is some problem when we are reading the data from the stream.
Please help what i made wrong here
Thanks