The getBlob() method of PreparedStatement is used to get Binary information.
Syntax:
------------
public Blob getBlob()throws SQLException
CREATE TABLE "IMGTABLE"
( "NAME" VARCHAR2(4000),
"PHOTO" BLOB
)
/
import java.sql.*;
import java.io.*;
public class RetrieveImage {
public static void main(String[] args) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","username","password");
PreparedStatement ps=con.prepareStatement("select * from imgtable");
ResultSet rs=ps.executeQuery();
rs.next();//now on 1st row
Blob b=rs.getBlob(2);
byte barr[]=new byte[(int)b.length()];//an array is created but contains no data
barr=b.getBytes(1,(int)b.length());
FileOutputStream fout=new FileOutputStream("d:\\anyforum.jpg");
fout.write(barr);
fout.close();
System.out.println("ok");
con.close();
}catch (Exception e) {e.printStackTrace(); }
}
}
4