AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-jdbc: How to retrieve image from database?

How can i retrieve image from database in java? Explain with source code.

java x 211
jdbc x 32
Posted On : 2013-11-28 20:33:07.0
profile Saksham Kumar - anyforum.in Saksham Kumar
73433936114
up-rate
5
down-rate

Answers


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(); }
}
}

Posted On : 2013-11-28 21:00:22
Satisfied : 1 Yes  1 No
profile Garima Gupta - anyforum.in Garima Gupta
596129554554
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in