aspose file tools*
The moose likes JDBC and the fly likes inserting BLOB values using prepared statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "inserting BLOB values using prepared statement" Watch "inserting BLOB values using prepared statement" New topic
Author

inserting BLOB values using prepared statement

Rohit Lal
Greenhorn

Joined: Sep 25, 2003
Posts: 20
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


Rohit Lal<br />SCPJ2 (2000)
Tim Morrow
Greenhorn

Joined: Sep 17, 2003
Posts: 11
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

Joined: Sep 25, 2003
Posts: 20
Thanks, this seems promising.. I'll try it out.
Rohit Lal
Greenhorn

Joined: Sep 25, 2003
Posts: 20
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)
{}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inserting BLOB values using prepared statement