Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

To store img in db (bytes) or not?

 
Ranch Hand
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using mySql.

my fear is not the size because it can not exceed 15-20k
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 755
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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].
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 think he's gonna try to grab my monkey. Do we have a monkey outfit for this tiny ad?
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic