Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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:
   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

 

                         

» View all related tutorials
Related Tags: java c memory time application object io runtime free method get order return this sso ai app with to run

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.