wood burning stoves 2.0*
The moose likes JDBC and the fly likes To store img in db (bytes) or not? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "To store img in db (bytes) or not?" Watch "To store img in db (bytes) or not?" New topic
Author

To store img in db (bytes) or not?

Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
Hi all,

I have 2 strategies to store a photo in a db (in my example of user's image)

1. save the image into a local folder (and make it unique - say by the userID --> 2052.jpg) to access it I just point to the portrai_folder\2052.jpg

or

2. I can convert the image to bytes (do String strDB = Base64.encode(bytes)) and store the string (strDB) in a database as field 'portrait'



Q: which one is better in terms of performance/efficiency (obviously #2 is easy to implement)

mind that images cannot exceed 15k (that is VARCHAR (20,000) )

Thanks for any pointers!
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
What db server are you using ? The big boys like oracle and sql server do have datatypes like LOB and BLOB which can hold large data [even movie clips]... so you wont be limited just to VARCHARs.
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
I'm using mySql.

my fear is not the size because it can not exceed 15-20k
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Even in MySql, you can use a datatype called LONGBLOB to store images. However, you will have to take care of the size constraint yourselves in the java code. Just out of curiosity, how do you plan to handle the size issue ? ie, if the image is more than 15K, are you going to discard it ?
Peter Primrose
Ranch Hand

Joined: Sep 10, 2004
Posts: 755
I'm going to resize the image first with

Graphics2D g = resizedImage.createGraphics();

* is it a good practice to store images in db or in a local folders?
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Peter, I dont think I'm authority enough to answer that question....

But, in my humble opinion, if your database server is in a clustered mode, ie, spread across multiple machines, then I suppose storing them in the database would be the better option.
If you are planning to store everything, viz., your db, and the folders in the same box, then storing the images in local folders would save your time for converting the LONGBLOB object to image, and then displaying it [a bit of performance improvement].
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Dawn Charangat wrote:But, in my humble opinion, if your database server is in a clustered mode, ie, spread across multiple machines, then I suppose storing them in the database would be the better option.

Yes, that's true. Although it would be possible to store the images in a drive which is accessed remotely from all the servers in the cluster.

This may or may not be a better option: there are other factors involved beyond performance and efficiency (whatever that means in this context). For example, if you keep the images separate from the database then it's possible for somebody to change or delete them, thus damaging your database integrity. Or it's possible that the drive containing the images becomes inaccessible because of a hardware problem, thus making them inaccessible even while the database is still OK.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To store img in db (bytes) or not?
 
Similar Threads
how to display images on JSP in struts2
save image on disk
Book Giveaway
Convert image to hide it
JDBC Connection