Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BLOB

 
Jyothsna Sri Y
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone...
Can somebody tell me how to extract an image file from a table in Oracle
using BLOB
Thankyou all
regards
Jyothsna
 
Steve Olson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is painful.
First of all, be sure your JDBC connection has auto commit set to false.
Upon insert or update replace column value with either EMPTY_CLOB() or EMPTY_BLOB() depending on underlying data type.
Next, select the Clob or Blob column from the database, in Oracle you can use ResultSet.getClob() or ResultSet.getBlob()
Now, using the Clob or Blob interface; get the appropriate OutputStream as in: outputStream=clob.getAsciiOutputStream(); (for Clob)
Next, write your data to the stream, close, flush and your are done!
Funny thing ... no need to update the row ... you've updated the clob after the select and actually written it to the database.
Have fun!
 
Steve Olson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just re-read post .. my instructions are for inserting or updating the LOB object (more difficult) ... reading is a bit easier ... just use ResultSet.getBlob() and read the binaryOutputStream from it ...
 
Jyothsna Sri Y
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Well I am sending the code along with this message... the two functions put an image into my database and get an image from my database respectively...
I am getting no Exceptions when I run the program.. but I am not getting the output also...
I tried to check the lenght of the Blob object.. it's giving me 0 as it's length...
Can u please check out where my code is going wrong....
/////////////////code here////////////////////
public void write(String desc) throws Exception
{
if (con == null) connect();
con.setAutoCommit(false);
CallableStatement stmt=con.prepareCall(sqlOut);
stmt.setString(1,desc);
stmt.registerOutParameter(2,java.sql.Types.BLOB);
stmt.executeUpdate();
BLOB blob=(BLOB) stmt.getBlob(2);
OutputStream orastrm=blob.getBinaryOutputStream();
BufferedInputStream filebuf=new BufferedInputStream(new
FileInputStream(desc+".jpg"));
int i;
while((i=filebuf.read())!=-1)
{
orastrm.write(i);
}
orastrm.flush();
con.commit();
stmt.close();
filebuf.close();
orastrm.close();
disconnect();
}
public void read(String desc)
{
if (con == null) connect();
PreparedStatement stmt=con.prepareStatement(sqlIn);
stmt.setString(1,desc);

ResultSet rs=stmt.executeQuery();
rs.next();
Blob picBlob=rs.getBlob(1);
byte[] picBuf = picBlob.getBytes(1,(int) picBlob.length());
stmt.close();
disconnect();
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic