0

I have a customer table in postgres and I want to insert the images for the customer data. I have images of the customers in a folder. I have named all the images name with their customerid.

Now I want to update customer table with their respective image in photo column which is of byte data type. Even though it's bad idea to store images in a database, my image size is small and I need to do that.

Can anybody please help me?

Son Truong
  • 12,343
  • 5
  • 28
  • 54
Mani Ratna
  • 828
  • 1
  • 9
  • 29

1 Answers1

1

I would start here for an understanding of processing the files in Java.
How to read all files in a folder from Java?

Then take a look at the docs on this topic at postgresql.org:
https://jdbc.postgresql.org/documentation/80/binary-data.html

For a database table like this:

CREATE TABLE images (imgname text, img bytea);

To insert an image, you would use:

File file = new File("myimage.gif");
FileInputStream fis = new FileInputStream(file);
PreparedStatement ps = conn.prepareStatement("INSERT INTO images VALUES (?, ?)");
ps.setString(1, file.getName());
ps.setBinaryStream(2, fis, (int)file.length());
ps.executeUpdate();
ps.close();
fis.close();
gahooa
  • 122,825
  • 12
  • 91
  • 98