3

Given an object X; I want this object to have an image. The image must be stored in the database. I can't store the path, the actual image must be in the database.

My question can be answered by answering the following subquestions:

a). What type of field should I put in the database? (e.g VARCHAR)

b) What type of object should I use for storing and manipulating the image (at an object layer)? (e.g java.awt.Image)

c) How do I create an object of the type selected (answer of question b) from the data obtained from the database?

d) How do I save an object of the type selected (answer of question b) to the database?

e) How do I draw the image on a web page?

I am using PostgreSQL, Java and it is a web application.

Thanks!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Step 1: Don't store images in your database. That's what file systems are for.. – Brendan Long May 16 '10 at 00:04
  • Yeah, I know. I've checked around and everyone says not to store it in the database but it is a school project and i am asked to do so. Thanks! –  May 16 '10 at 03:13
  • 1
    storing images in the database is fine, and it can simplify your backup/restore plan if all your eggs are in one basket. having said that, if you're using this database to back serving images over HTTP, you want some sort of cache outside the DB. – araqnid May 16 '10 at 13:15

2 Answers2

5

a) What type of field should I put in the database? (e.g VARCHAR)

An image is binary data. Just use a binary field. In PostgreSQL it's bytea.

b) What type of object should I use for storing and manipulating the image (at an object layer)? (e.g java.awt.Image)

Use an InputStream or byte[] to store it. The Java 2D API has classes/methods which can take/return either of those types.

c) How do I create an object of the type selected (answer of question b) from the data obtained from the database?

Use ResultSet#getBinaryStream() to get an InputStream of it or ResultSet#getBytes() to get a byte[] of it.

d) How do I save an object of the type selected (answer of question b) to the database?

Use PreparedStatement#setBinaryStream() or PreparedStatement#setBytes(). Note: if you're new to JDBC/PreparedStatement as well, then I recommend you to get yourself through the Sun basic JDBC tutorial.

e) How do I draw the image on a web page?

Use a HTML <img> element whose src points to an URL matching the url-pattern of a Servlet. You can pass the image identifier as request parameter or pathinfo. Inside the Servlet you just write the obtained InputStream to the OutputStream of the response the usual Java IO way. You can find a code example in this answer I posted some time ago.


That said, storing only raw images in a database is generally not a good idea. You should preferably also store some metadata along it, such as the content type (e.g. image/jpeg, image/gif, etc) which you after all need in the response header so that the browser knows what to do with it.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

You can save an object as a PostGreSQL blob. They can stored as binary data in the database. http://www.postgresql.org/files/documentation/books/aw_pgsql/node96.html can help. Then you can write the binary input using Servlet OutputStream. This link might help.

Kartik
  • 2,541
  • 2
  • 37
  • 59