Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Get Blob

Advertisement
Blob is known as 'binary large object', is a collection of binary data stored as a single entity in a database management system and can hold variable amount of data.

Get Blob

     

Blob is known as 'binary large object', is a collection of binary data stored as a single entity in a database management system and can hold variable amount of data. Here in the example shown below we will learn how to retrieve image from mysql database which is saved as large binary object. 


Description of the code:
   1.
First create connection to the database from where you want to retrieve image saved as blob.
   2. Create a mysql query to retrieve image value from table column.
   3. Retrieves the value of the designated column in the current row of this ResultSet object as a Blob object by using getBlob() method. 
   4. Write data on standard output device.

Table Structure:

CREATE TABLE pictures ( image_id
 int(10) NOT NULL auto_increment,
image blob
)

Here is the code of GetBlob.java

import java.io.*;
import java.sql.*;

class GetBlob {
FileOutputStream image;
Connection con = null;
PreparedStatement pstmt = null;
Statement stmt= null;
ResultSet res = null;
StringBuffer query=null;
String driverName = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/";;
String dbName = "register";
String userName = "root";
String password = "root";

public GetBlob(){
try{
Class.forName(driverName);
con = DriverManager.getConnection(url+dbName,userName,password);
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from picture where image_id='3'");
if (rs.next()) {
Blob test=rs.getBlob("image");
InputStream x=test.getBinaryStream();
int size=x.available();
OutputStream out=new FileOutputStream("C:\\anu.jpg");
byte b[]= new byte[size];
x.read(b);
out.write(b);
}
}
catch(Exception e){
System.out.println("Exception :"+e);
}
finally{
try{
stmt.close();
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
public static void main(String args[]) throws IOException{
GetBlob blob = new GetBlob();
}
}


Image retrieved from the database:

Download Source Code

 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 23, 2008

Related Tutorials

Ask Questions?    Discuss: Get Blob   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Venkateswarlu
April 1, 2011
Getting Error

After executing the above code i am getting file unsupported Exception what should i do please help me in retrieving the image from the database without using servelet
chinna
February 22, 2012
could not load image

hi, im trying ot do the same way but my image could be loaded. below is my code ____________________________________________________________ httpTransport.call(SOAP_ACTION, envelope); Object response = envelope.getResponse(); test = response.toString(); Blob picture = org.hibernate.Hibernate.createBlob(test.replaceAll("-", "").getBytes()); String FILENAME = "voucher1.jpg"; File root = Environment.getExternalStorageDirectory(); FileOutputStream f = new FileOutputStream(new File(root, FILENAME)); InputStream x=picture.getBinaryStream(); int size=x.available(); byte b[]= new byte[size]; x.read(b); f.write(b); f.close();
Majid
March 26, 2012
thanks

is was so useful for me... thanks alot
DMCA.com