Saving image in postgresql

I am trying to save an image in a Postgresql database, I have a procedure that is used to store an image in a database, the image column type is bytea.I tried to convert my image to String (Base64) and convert it to byte [], but failed refresh this image.

Code for reading an image and converting it to a string: This: -

 File file = new File("FilePath\\image.jpg");

 try
 {
     // Reading a Image file from file system
     FileInputStream imageInFile = new FileInputStream(file);
     byte imageData[] = new byte[(int) file.length()];
     imageInFile.read(imageData);

     // Converting Image byte array into Base64 String By calling encodeImage Function
     byte[] imageDataString = encodeImage(imageData);

     CallableStatement statement = con.prepareCall(" { call products_update_image( '" + id + "', '" + imageDataString + "') } ");
     statement.execute();
 }
 catch (Exception ex)
 {
     System.out.println("Exception is:- " + ex);
 }

 public static byte[] encodeImage(byte[] imageByteArray)
 {
     return Base64.encodeBase64(imageByteArray);
 }

I used this link to convert the image. Link The following is the procedure that is used to save the image in the database.

CREATE OR REPLACE FUNCTION UpdateProfileImage(product_id character varying, img bytea)

Can someone tell me why I cannot save this image or what I am doing wrong.

+4
1

a_horse_with_no_name. . .

PreparedStatement pstmt = con.prepareStatement("UPDATE PRODUCTS SET IMAGE = ? WHERE ID = ?");
File file = new File("C:\\Program Files (x86)\\openbravopos-2.30.2\\image.jpg");
FileInputStream in = new FileInputStream(file);
try
{
    pstmt.setBinaryStream(1, in, (int) file.length());
    pstmt.setString(2, id);
    pstmt.executeUpdate();
    //con.commit
}
catch (Exception ee)
{
    System.out.println("Exception is:- " + ee);
}
+4

All Articles