• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inserting BLOB values using prepared statement

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I need to store an ArrayList into the d/b for later processing. I was
trying to store it as BLOB type.
I used the oracle.sql.BLOB.createTemporary(Connection,boolean,int) method
to create a BLOB object into which I would store the ArrayList.
But this statement throws a NullPointerException, even though
the Connection object I pass is not null.
Help!!
Rohit
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not really sure what the purpose of BLOB.createTemporary() is for, but if you need help in figuring out how to read and write BLOBs in Oracle, you might check out this link:
http://otn.oracle.com/sample_code/tech/java/sqlj_jdbc/files/advanced/LOBSample/Readme.html
 
Rohit Lal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, this seems promising.. I'll try it out.
 
Rohit Lal
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
{}
}
}
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic