0

My problem is the following: so I have a database that has some customers stored in it. For every customer I added a picture like this:

INSERT INTO ITSO.CUSTOMER (TITLE,FIRST_NAME,LAST_NAME,SSN,IMAGE) VALUES ('Mr','Henry','Cui','111-11-1111', LOAD_FILE('D:/Workspace8/Images/11.jpg'));

How can I retrieve these pictures in an Enterprise java bean? ( I have to mention I am using JPAs). Are the queries a solution?

@NamedQuery(name="getImageForCustomer", query="select image from Customer c where c.ssn=?1")

Do I need to store my picture in a different way? In the EJB I have this method getCustomer(). Can I add another argument, something like Byte[] image??

public Customer getCustomer(String ssn) throws ITSOBankException {
    System.out.println("getCustomer: " + ssn);
    try {           
    return entityMgr.find(Customer.class, ssn);
    } catch (Exception e) {
        System.out.println("Exception: " + e.getMessage());
        throw new ITSOBankException(ssn);
    }

My plan is to display the image in a JSP afterwards, using a servlet and an EJB injection.

I would be so grateful if someone can help me!!!

(It's for learning purpose)

Boa0210
  • 19
  • 3

1 Answers1

0

First of all, storing image in database is not very good practice. Though it is sufficient for learning purposes, in real application consider storing images in file system or cloud service of your choice and store only path or link to image.

But back to your question. In your Customer JPA class, you can define field for image annotated with javax.persistence.Lob annotation:

@Lob private byte[] image;

...and your image data will be loaded into image byte array field by JPA.

To display image on JSP page you have to implement servlet, which will provide image data based on customer id (for example /image?customerId=10). Here is a good example how to implement such a servlet: How to convert byte array to image

Community
  • 1
  • 1
sider
  • 193
  • 6