File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes BLOB Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "BLOB" Watch "BLOB" New topic
Author

BLOB

Jyothsna Sri Y
Ranch Hand

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

Joined: Dec 04, 2003
Posts: 7
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

Joined: Dec 04, 2003
Posts: 7
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

Joined: Sep 25, 2003
Posts: 35
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();
}
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

http://otn.oracle.com/sample_code/tech/java/sqlj_jdbc/files/advanced/advanced.html


Groovy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BLOB
 
Similar Threads
how to convert byte[] to blob
JSP - SQLServer
How to store Objects in Database?
interview question
how to store object in a table