Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Save image to DB2 as a BLOB?
Old 10-06-2006, 11:45 AM Save image to DB2 as a BLOB?
RickPlmr's Avatar
Super Talker

Posts: 107
Name: Rick Palmer
Trades: 0
I have a JSP file upload page that will receive images, which I then need to save to a DB2 database. What are some recommended approaches for doing this?

I suspect it will be some process of getting the uploaded image as a binary stream, and then streaming it to the database field, but any code samples or helpful articles would be greatly appreciated .

Thanks for the help,
__________________

Please login or register to view this content. Registration is FREE
- free online training in Java, J2EE, and MySQL.


Please login or register to view this content. Registration is FREE
- answers and advice from a geek who knows stuff.
RickPlmr is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-07-2006, 06:51 AM Re: Save image to DB2 as a BLOB?
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,517
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Quote:
What are some recommended approaches for doing this?
Don't

Retrieving images as BLOBs from a database is highly resource intensive and will dramatically slow down your site pages.
Simply store the path in the table field and the image on disk.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-30-2006, 01:21 PM Re: Save image to DB2 as a BLOB?
RickPlmr's Avatar
Super Talker

Posts: 107
Name: Rick Palmer
Trades: 0
That has plenty of problems of its own, such as maintaining those links (especially when someone decides to change folder structures and forgets to tell the development team).

I was actually able to get the BLOB-from-DB2 approach to work, and it seems to be performing well for the type of traffic that the site will be used for. I think many of us are developing small to medium traffic sites where a few added milliseconds to load a photo isn't going to be very noticeable.

The trick was to use a servlet to handle the retrieval of the blob field from the database (which calls out to an EJB, which calls to a DAO) and map the html page's <img> tag to the servlet. Assuming a servlet that was mapped as "photo" in web.xml, notice how I point the image tag's src attribute to the servlet and pass in the id of the photo (in this case loading pictures of vehicles identified by VIN numbers):

<img id="photoImg" src="photo?serial=<%=vehicle.getVIN()%>" border="0" />
(The above code would go in a jsp page.)

Once the blob is retrieved by the servlet, then it gets converted to a byte array, which is then written directly to the servlet's outputstream:

Code:
public static byte[] getBytesFromBlob(Blob p_blob) throws Exception
    {
  int pos = 1;
        int len = (int) p_blob.length();
        byte[] bytes = p_blob.getBytes(pos, len);
        return bytes;
    }
__________________

Please login or register to view this content. Registration is FREE
- free online training in Java, J2EE, and MySQL.


Please login or register to view this content. Registration is FREE
- answers and advice from a geek who knows stuff.
RickPlmr is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Save image to DB2 as a BLOB?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.20956 seconds with 12 queries