Skip to Main Content

Java Database Connectivity (JDBC)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Need help in copying BLOB data from one table to another

user778210-OracleJul 26 2016 — edited Jul 29 2016

We store images in oracle database as BLOB datatype. I have following usecase:

1) Read the image data from table1 and store the value in an .xml file.

2) Read the .xml file and store the image  data back in table 2

This is the code that I am using to achieve this :

ResultSet rs = stmt.executeQuery("select * from WC_PEOPLE_CONN_PROFILE_PHOTO");
  while (rs.next())
  {
    Blob ablob = rs.getBlob(4);
    String imageDataString = encodeImage(ablob.getBytes(1L, (int) ablob.length()));
    // Converting a Base64 String into Image byte array
    byte[] imageByteArray = decodeImage(imageDataString);
  
    PreparedStatement pstmt = con2.prepareStatement(
       "insert into wc_people_conn_profile_photo (id,user_guid,photo_size,image_data) values (?,?,?,?)");
    //InputStream in = new ByteArrayInputStream(imageByteArray);
    con2.setAutoCommit(false);

    pstmt.setString(1, "17");
    pstmt.setString(2, "vicki7");
    pstmt.setString(3, "original");
    pstmt.setBinaryStream(4, in, (int)imageByteArray.length);
    pstmt.executeUpdate();
    con2.commit();
  }
  con.close();
  con2.close();

  System.out.println("Image Successfully Manipulated!");
}
catch (Exception e)
{
  System.out.println("Image not found" + e);
}

  }

 

public static String encodeImage(byte[] imageByteArray)

  {

return Base64.encodeBase64URLSafeString(imageByteArray);

  }

 

public static byte[] decodeImage(String imageDataString)

  {

return Base64.decodeBase64(imageDataString);

  }

When i try to view the uploaded image in table2 using SQL Developer, I am seeing following error -

"Image could not be decoded from the binary stream"

What is the right way to read and write the BLOB data from databases

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 26 2016
Added on Jul 26 2016
9 comments
3,343 views