Introduction
It’s possible to use images stored in a database for your reports. Images stored in a database as stored as BLOBs, or in the case of Postgres as a BYTEA. The image gets stored as one of these binary data types.
This article goes over an example that does this, and which you can download.
File system vs. database storage for images
The usual strategy for storing images is to store image files on a file system and then have references to their file location in the database. Storing a reference in the database hardly takes up any space and having the file system host the images doesn’t require any extra processing when being served up by web servers.
Also, applications that use images stored in databases need to generate the right query to pull the image and then may need to do some post-processing to use the image in an application, which adds to the response latency, as well as adding load to the database.
On the other hand, storing images in a database has its advantages. It’s easier to backup the images using the database’s own functions, provide transactional integrity (if necessary), and storing them in a database adds an extra layer of security – it’s a lot harder to get at them if they’re in a database than if they’re in a file system.
Most of the time, it will make more sense to have the images stored in the file system, but you should be aware of situations where it may be advantageous to store them in a database.
What to do in the report design
You’ll design your report query as you normally would, selecting the image field as part of the SQL statement.
Next, you’ll need to set the field class type to java.awt.Image. The screenshot below is from iReport, in Jaspersoft Studio, it’s a similar process.
Next, add an Image element from the palette to the band in the report design and then modify the the Image Expression class to java.awt.Image (thus matching the field type), and set the Image expression to the field holding your image.
As you can see in the image below, the expression class is set to java.awt.Image.
Example
The example attached to this page requires you to download the ZIP file and unzip it to C:TEMP (if you don’t, you’ll need to modify the image-loading commands) and have either MySQL or Postgres.
Choose the MySQL or Postgres scripts, which will create a sample table in the database and store the images in the database.
Once you complete the database steps, open up the JRXML file and execute it – you’ll need to make sure you’re pointing at the right database, and you should be good to go.
Recommended Comments
There are no comments to display.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now