Home > DeveloperSection > Beginner > Working With BLOB(Binary Large Objects)

Working With BLOB(Binary Large Objects)


Database Database  Java  J2EE  Database Design  Database Schema  Database Table 
Ratings:
0 Comment(s)
 1092  View(s)
Rate this:
 


Working With BLOB(Binary Large Objects)



For taking a example of BLOB is storing image into the database, BLOB (Binary Large Object) datatype is used in the table. i.e.

we can store images in the database in java by the help of PreparedStatement interface.

The setBinaryStream() method of PreparedStatement is used to set Binary information into the parameterIndex.


 
Now we have write the jdbc code to store the image in the database. Here we are using ' C:\\Users\\sony\\Pictures\\Saved Pictures\\anu.png'  for the location of image.

// Code for Storing Image In the Database
PreparedStatement ps=con.prepareStatement("insert into imgtable values(?,?)");  
ps.setString(1,"Anupam Mishra");  
  
FileInputStream fin=neFileInputStream("C:\\Users\\sony\\Pictures\\Saved Pictures\\anu.png");  
ps.setBinaryStream(2,fin,fin.available());  
int i=ps.executeUpdate();   
Now , We used Second code  for Retrieving Inserting Image are as follow:


//Code for Retrieving Image from the Database
PreparedStatement ps=con.prepareStatement("select * from imgtable");  
ResultSet rs=ps.executeQuery();  
if(rs.next()){//now on First row  
              
Blob b=rs.getBlob(2);//2 means Second column data  
byte barr[]=b.getBytes(1,(int)b.length());//1 means first image  
              
FileOutputStream fout=new FileOutputStream("C:\\Users\\sony\\Pictures\\Saved Pictures\\ImgDownload.png");  
fout.write(barr);  
              
fout.close();  
}
System.out.println("Image Retrieve Succesfully ");  
      
For output of this program in java are as follow:-


 

Don't want to miss updates? Please click the below button!

Follow MindStick