Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Keeping an image in database

 
Mousa A. Rafi
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To keep things simple, assume that I want a database where to store different peoples name along with their photos (images - .jpg). The MySQL server to be used for this purpose is located in a remote machine. My Java program should be able both save new person info (i.e. name and image) to the server and also retreive those info and show them using a JTable.

Now, what's the best way to do it? I can think of two: 1. save the iamge in database using BLOB or 2. save the image in a directory in remote machine and a link to that file in the database.

Whatever way it is, I don't have a clue how to do it. I neither used BLOB types nor know how to and not sure what protocol to use for sending and retreiving files on a network that'll be suitable for java.
 
Steven Bell
Ranch Hand
Posts: 1071
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little weak on using Blobs, but you should be able to pass a byte[] or stream as part of a sql insert statement to create the blob. You wouldn't have to worry about any special protocols, it would all be done with the DB connetion.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mousa,
I agree that using the database is the simplest approach. Steven is correct that you can use byte[].
 
Mousa A. Rafi
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the kind suggestions. Working with it, seeing other's code example I feel the same. Actually working with PHP and web technology made me think of the other approach.

I've settled with my approach and worked a bit with it as well. But, both inserting and retreiving seems a lot of work which I'm not comfortable with. It'd be helpful if anyone could show me some generalized steps towards acheiving the goal, so that I can logically settle it in my mind.
 
asirob civokviz
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not save images in database if that is not absolutely necessary!!!
It is much better (IMHO) to save images in file system and only paths to
them in database!!

regards...
 
S Herod
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agress with asirob

Storing blobs in a database leads to pain. If you are using oracle the are all sorts of rules about tables with blobs that stop you from doing things you could otherwise do (like partitioning).

On our production systems, this storing of data in BLOB types has causes us SIGNIFICANT issues as the db scales (250GB and grow, most of it BLOBs)

Store the path to the image on the file system, in the long run you will be better off!
 
Mousa A. Rafi
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice to see different opinions. Well, if I were to build this database for a intranet or web solution that works with general HTML, HTTP protocol and probably a scripting language like PHP, I would definitely go for saving links to the files rather than BLOB. Even if the Java application was to run on the same machine where the DB server is running I would do the same. But, when it comes to building an application with Java that should work on a network and the DB server is located in a different machine then using BLOB seems easier. However maintaining the database is also an issue and hence how I can do it with links is something I'd really love to know. Thanks once again to all for your responses.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic