Sample to recover picture from Oracle database
By the offer of PreparedStatement we some assistance with canning recover and store the picture in the database.
The getBlob() strategy for PreparedStatement is utilized to get Binary data, it gives back the occurrence of Blob. In the wake of calling the getBytes() technique on the blob object, we can get the variety of parallel data that can be built into the picture record.
Mark of getBlob() strategy for PreparedStatement
open Blob getBlob()throws SQLException
Mark of getBytes() strategy for Blob interface
open byte[] getBytes(long pos, int length)throws SQLException
We are accepting that picture is put away in the imgtable.
Make TABLE "IMGTABLE"
( "NAME" VARCHAR2(4000),
"Photograph" BLOB
)
/
Presently we should compose the code to recover the picture from the database and compose it into the catalog so it can be shown.
In AWT, it can be shown by the Toolkit class. In servlet, jsp, or html it can be shown by the img tag.
import java.sql.*;
import java.io.*;
open class RetrieveImage {
open static void main(String[] args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Association con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
PreparedStatement ps=con.prepareStatement("select * from imgtable");
ResultSet rs=ps.executeQuery();
if(rs.next()){//now on first line
Blob b=rs.getBlob(2);//2 implies second section information
byte barr[]=b.getBytes(1,(int)b.length());//1 implies first picture
FileOutputStream fout=new FileOutputStream("d:\\sonoo.jpg");
fout.write(barr);
fout.close();
}//end of if
System.out.println("ok");
con.close();
}catch (Exception e) {e.printStackTrace(); }
}
}