Adsense Ad

Tuesday, 30 January 2018

How to insert image on oracle blob column from sqlite blob column using jdbc in android

In this article we learn how to insert image on oracle blob column(table created on server) from sqlite blob column (mobile db) using jdbc in android.
For connecting mobile app to oracle database via jdbc, You can read my previous article
"How to connect Android App with Oracle Database".

we assume that image was already save in mobile sqlite db in table named as "and_db_img" and now we retrieve images from sqlite db and save it in oracle db table named as "ora_db_img" using jdbc:

Sample Code:

String sql = "INSERT INTO ora_db_img(Image_ID,image_file) VALUES (?,?)";

PreparedStatement ps;
ps = connection.prepareStatement(sql);
byte[] imageByteArray;
ByteArrayInputStream imageStream;


Cursor cursor = databaseHandler.getData("select imgId,ImgFile from and_db_img");

int 
cntr=0;

if (
cursor.getCount() > 0){
while (
cursor.moveToNext()) {
   cntr++;
   ps.setString(1,cursor.getString(0));  
 imageByteArray=cursor.getBlob(2);   
imageStream = new ByteArrayInputStream(imageByteArray);
ps.setBinaryStream(2,imageStream); 
ps.execute();
 }
}

No comments: