permaculture playing cards*
The moose likes JDBC and the fly likes Keeping an image in database 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 "Keeping an image in database" Watch "Keeping an image in database" New topic

Keeping an image in database

Mousa A. Rafi

Joined: Jan 30, 2005
Posts: 28
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

Joined: Dec 29, 2004
Posts: 1071
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
internet detective

Joined: May 26, 2003
Posts: 30050

I agree that using the database is the simplest approach. Steven is correct that you can use byte[].

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Mousa A. Rafi

Joined: Jan 30, 2005
Posts: 28
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

Joined: Jun 12, 2004
Posts: 24
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!!

S Herod

Joined: Apr 25, 2005
Posts: 10
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

Joined: Jan 30, 2005
Posts: 28
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.
It is sorta covered in the JavaRanch Style Guide.
subject: Keeping an image in database
Similar Threads
how to save image to mysql?
Sending and handling files in a network
storing images in the database
string to blob-please help
Uploading an Image