Retrieving image from Oracle database

Retrieving image from Oracle database

Get the image from the table into blob class object.
Blob b = rs.getBlob(1);
Here, 1 represents the column from where the image is retrieved and stored into b.
Creare byte array and store the the image from b into the byte array.
byte arr[] = new byte[size of image];
arr = b.getBytes(1,(int)b.length());

Write this byte array arr into a file.
FileOutputStream fos = new FileOutputStream(java.jpg);
fos.write(arr);

Example
import java.sql.*;
import java.io.*;
public class test
{
public static void main(String[] args)
{
try{
Class.forName(“oracle.jdbc.driver.OracleDriver”);
Connection con=DriverManager.getConnection( “jdbc:oracle:thin:@localhost:1521:xe”,”system”,”system”);
PreparedStatement ps=con.prepareStatement(“select * from imgtab”);
ResultSet rs=ps.executeQuery();
if(rs.next())
{
Blob b=rs.getBlob(2);
arr[]=b.getBytes(1,(int)b.length());
FileOutputStream fos=new FileOutputStream(“d:\\java.jpg”);
fos.write(arr);
fos.close();
}
System.out.println(“ok”);
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *