Store image into and load image from JavaDB using EclipseLink

My successful experience on how to store and load an image (.jpg) using JavaDB+EclipseLink+Java 1.6.0_25 and NetBeans 7.0 is as follows.

  1. A table named, Customer, is used as an illustrating example. There are three attributes in this table: CustomerId, Name, and Photo.
  2. An SQL script of this table is shown below:

    create table APP.Customer
    CUSTOMER_ID INTEGER not null primary key generated always as identity (start with 1, increment by 1),
    PHOTO BLOB(2147483647)

  3. Create a database named, CustDB, through the Services tab of NetBeans IDE.
    1. In NetBeans IDE 7.0, click on the Services tab.
    2. Right-click on the JavaDB node, and then click on Create Database…
    3. Enter the database name, user name and password in the provided text boxes. Use “mydb” for the user name and password.
  4. Create a new connection to the newly-created database.
    1. On the “Services” tab, right-click on the “Databases” node, and then click on “New Connection…”
    2. We will use JavaDB Network for this tutorial, so select “JavaDB (Network)” for the driver. Then, click Next.
    3. Enter Host Name: localhost, Port: 1527, Database: CustDB, User name: mydb, and Password: mydb.
    4. Click Next
    5. Select “APP” as the schema name
    6. Click Finish
    7. A new connection node: jdbc:derby:localhost:1527/CustDB [myapp on APP]
  5. Create a new table in CustDB
    1. Expand the CustDB connection node
    2. Expand the APP node under CustDB node
    3. Right-click on the “Table” node under the CustDB/APP node, and click on “Execute Command…” to open an SQL window
    4. Copy the SQL script shown above into the SQL window, and click “Execute” icon.
  6. Create Java Library Project
    1. On the Netbeans IDE 7.0 menu, File –> New Project
    2. Categories: Java and Projects: Java Class Library
    3. Click Next
    4. Project Name: MyCustomerLibraryProject
    5. Click Finish
    6. On the Project tab, right-click on the MyCustomerLibraryProject node –> New –> Entity Classes from Databases …
    7. Select the CustDB connection that is just created above for the Database Connection. Then, there should be only one available table, named Customer.
    8. Click “Add All” and then click “Next”
    9. Put “demo” as the package name, and then click “Finish”
  7. Modify the generated Customer.class file in the MyCustomerJavaLibraryProject
    1. Click the “demo” node to expand the node, there will be one Java file named, “”.
    2. Click the file to open it in the NetBeans IDE editor.
    3. Since we use “mydb” as user name and password, we need to use a fully-qualified name for the table name. So, change:

      @Table(name = "CUSTOMER")


      @Table(name = "APP.CUSTOMER")

      The reason is that JavaDB will presume that the user name, in this case “mydb”, is the schema name, however, we create the Customer table in “APP” schema. As a result, we need to use a fully-qualified name for the Customer table which is “APP.CUSTOMER”.

    4. In JavaDB, EclipseLink maps “BLOB” SQL data type into “Serializable” Java SQL data type. However, to store an image, we need to use “byte[]” Java SQL data type. Accordingly, we need to change:
      • The data type of “photo” variable definition from Serializable to byte[].

        @Column(name = "PHOTO")
        private Serializable photo;


        @Column(name = "PHOTO")
        private byte[] photo;
      • The returned data type of getPhoto() method from Serializable to byte[]

        public Serializable getPhoto() {
        return photo;


        public byte[] getPhoto() {
        return photo;
      • The parameter data type of setPhoto() from Serializable to byte[]

        public void setPhoto(Serializable photo) { = photo;


        public void setPhoto(byte[] photo) { = photo;

    5. Now, we are ready to generate MyCustomerLibrary.jar by right-clicking on the “MyCustomerLibraryProject” and then click “Clean and Build”
  8. Create a Library Wrapper module containing “MyCustomerLibraryProject.jar” created above. Then, set the dependencies of this module to DerbyClient and EclipseLink library wrapper modules. Assume that these two modules are created beforehand.
  9. Now, in a Java code where we need to save an image from JLabel into our CustDB database in JavaDB, use the following code snippet.

    //Store image from jLabel into JavaDB
    Customer newCustomer = new Customer();
    ImageIcon imgIcon = (ImageIcon) jLabelImage.getIcon();

    if (imgIcon != null) {

    Image img = imgIcon.getImage();
    byte[] imgByte = getByteArrayFromImage(img);


  10. The getByteArrayFromImage() method is defined as follows.

    private byte[] getByteArrayFromImage(Image img) {

    byte[] imgByte = null;

    try {
    int imgWidth = img.getWidth(null);
    int imgHeight = img.getHeight(null);
    int imgType = BufferedImage.TYPE_INT_ARGB; // you can experiment with this one
    BufferedImage bi = new BufferedImage(imgWidth, imgHeight, imgType);
    Graphics2D g2 = bi.createGraphics();
    g2.drawImage(img, 0, 0, null);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    if (ImageIO.write(bi, "JPEG", baos)) {
    imgByte = baos.toByteArray();

    } catch (IOException ex) {

    return imgByte;


  11. In addition, for the Java code where an image is loaded from CustDB and displayed in JLabel, use the following code snippet:

    //For image from database to jLabel
    Customer existingCustomer = getCustomerFromDB(customerID);//load a Customer object from database
    byte[] imageByte = existingCustomer.getPhoto();
    if (imageByte != null) {

    ImageIcon imageIcon = new ImageIcon(imageByte);
    java.awt.Image img = imageIcon.getImage();



  12. That’s pretty much all we need to do. However, you may need to specify allowable image file types, and scale an image before putting in a JLabel.
  13. The methods in steps #9, #10 and #11 can be used for MySQL database. The thing is that MySQL maps BLOB SQL data type into byte[] Java SQL data type. Therefore, we do not need to do step #7.
  14. The most important steps are #7, #9, #10 and #11 that we need to modify file and define save/load methods.

Have fun!

This entry was posted in Uncategorized. Bookmark the permalink.

5 Responses to Store image into and load image from JavaDB using EclipseLink

  1. Nice Tutorial…
    Can you please provide a download link of tutorial.

  2. zahra says:

    thanks a lot

  3. Pavan says:

    i am Pavan thanks it helps

  4. ISMAIL says:

    HI, I’m ISMAIL. Your tutorial just saved me the trouble of having to call a servlet to retrieve my images from database inorder to place in jsp . I’m creating web application with ejb 3.0, entity beans, jsp, javaDB. Spent ages looking for this simple solution. BIG THANKS

  5. ZED says:

    Thanks a lot, This blog helped

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s