Share on Google+Share on Google+

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.

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:
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

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(){
con = DriverManager.getConnection(url+dbName,userName,password);
ResultSet rs=stmt.executeQuery("select * from picture where image_id='3'");
if ( {
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];;
catch(Exception e){
System.out.println("Exception :"+e);
catch(Exception e){
public static void main(String args[]) throws IOException{
GetBlob blob = new GetBlob();

Image retrieved from the database:

Download Source Code



Posted on: October 23, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: Get Blob   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
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
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 ____________________________________________________________, 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];; f.write(b); f.close();
March 26, 2012

is was so useful for me... thanks alot