Hi,
I finally managed to get the solution for the mentioned problem. Here is
the code for storing and then retrieving a BLOB value. In this example
I'm storing an ArrayList of values. Please take care to import oracle.sql.BLOB and oracle.jdbc.OracleResultSet. Keep classes12.zip for the necessary packages in your classpath to access these Oracle-specific classes.
Also, this "test()" method was running inside a Session Bean. In case yours is not a session bean or anything that doesn't have access to the container, just take care of getting the InitialContext properly in your code so that lookups can be performed.
private void
test()
{
DataSource dataSource = null;
Connection connection = null;
PreparedStatement ps = null;
String sql = null;
try
{
Context ic = new InitialContext();
dataSource = (DataSource)ic.lookup("jdbc/TST92DS");
connection = dataSource.getConnection();
ArrayList list = new ArrayList();
HashMap m1 = new HashMap();
m1.put("name","ratan");
HashMap m2 = new HashMap();
m2.put("name","amit");
HashMap m3 = new HashMap();
m3.put("name","munish");
HashMap m4 = new HashMap();
m4.put("name","surinder");
list.add(m1);
list.add(m2);
list.add(m3);
list.add(m4);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(list);
oos.flush();
oos.close();
oos=null;
byte[] byteArray = bos.toByteArray();
sql = "insert into rm_accruals values (1, ?, 'N', sysdate, '1')";
ps = connection.prepareStatement(sql);
ps.setBytes(1,byteArray);
ps.executeUpdate();
ps.close();
ps=null;
Statement stmt = connection.createStatement();
String qSql = "select ACCRUALS_DATA from rm_accruals where accruals_i = 1";
ResultSet rs = stmt.executeQuery(qSql);
byte[] inbyteArray = null;
BLOB oracleBlobObj = null;
while(rs.next())
{
oracleBlobObj = ((OracleResultSet)rs).getBLOB(1);
}
rs.close();
rs=null;
stmt.close();
stmt=null;
int length = (int)oracleBlobObj.length();
byte[] bytes = oracleBlobObj.getBytes(1l,length);
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
ArrayList newlist = (ArrayList)ois.readObject();
ois.close();
ois=null;
for(int i=0;i<newlist.size();i++)
{
System.out.println(((HashMap)newlist.get(i)).get("name"));
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
sql = "delete from rm_accruals";
ps = connection.prepareStatement(sql);
ps.executeUpdate();
ps.close();
ps=null;
connection.close();
connection=null;
}
catch(Exception e1)
{}
}
}