Table of contents
WHAT IS A BLOB AND NEED OF IT?
A blob is a binary large object, name itself blob is used to store large binary data in the form of binary streams.
BLOB is mostly used in SQL(StructuredQueryLanguage) to store unstructured data files. These files include images of type jpg, jpeg, png,webp etc.. and videos like mp3,mp4 etc..and a blob can handle up to 4GB.
preparestatement.setBlob(index,fileInputStreamObject);
INSERTION
Let’s understand it in a simpler way
In the above diagram initially, the image object is converted into a .java file and then through preparestatement object (by calling the .setBlob() method) we store the image in our desired database.
It is stored in binary format in our database.
Here is an example of inserting an image into the MySQL database.
Source code:-
import java.io.*;
import java.sql.Connection.*;
import java.sql.PreparedStatement.*;
import java.util.Scanner.*;
Public class BlobDemo{
Connection con=null;
PreparedStatement pstmt=null;
String query=null,name=null;
Scanner sc=new Scanner(Syatem.in);
public static void main(String [ ] args){
String url=”jdbc:mysql://localhost:3306:/mydb;
String user=”root”;
String password=”mypassword”;
try{
//establishing database connection
con=DriverManager.getConnection(url,user,password);
if(con!=null){
query=”insert into blobtable (name,image) values(?,?)”;
pstmt=con.prepareStatement(query);
}
if(pstmt!=null){
System.out.println(“enter the name:”);
name=sc.nextInt();
//image file is reaching to java application
File f=new File(“natureimg.jpg”);
FileInputStream fis=new FileInputStream(f);
//setting first index to string
pstmt.setString(1,name);
//setting the input from .java to our database
pstmt.setBlob(2,fis);
int rows=pstmt.executeUpdate();
if(rows>0){
System.out.println(“no.of rows inserted: ”+rows);
}
else{
System.out.println(“insertion can’t be done!!”);
}
System.out.println(“absolute file path:”+f.getAbsolutePath());
}catch(SQLException s){
s.printStackTrace();
} catch(FileNotFoundException f){
f.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}finally{
if(con!=null){
con.close();
}
if(pstmt!=null){
pstmt.close();
}
}
}
}
In the above example, I have inserted an image into the database using a blob
Initially, I created a connection and preparedstatement objects and set them to null.
Further, I connected to the MySQL database using a URL, username and password.
Here through the file and fileInputstream object, file is reaching my .java application.
Note:-Here to know to print the absolute path of my file I used File object, usually, we can make use of fileInputStream directly like:-
FileInputStream fis=new FileInputStream(“filename”);
RETRIEVAL
To retrieve a blob object from our database:-
We already know that the blob data is stored in binary format in our database, to reach it into the .java file we use InputStream object.
After reaching it into the .java file object we need to keep it in our drive(hard disk).
To store it in our drive we use OutputStream object.
Following is the example of retrieving blob data from our database:-
import java.sql.*;
import java.io.*;
Public class BlobRetreival{
Connection con=null;
PreparedStatement pstmt=null;
ResultSet resultset=null;
String name=null;
public static void main(String [ ] args){
String url=”jdbc:sql://localhost:3306:/mydb;
String user=”root”;
String password=”mypassword”;
try{
con=DriverManager.getConnection(url,user,password);
String query=”select * from blobtable where name=?”;
if(con!=null){
pstmt=con.prepareStatement(query);
}
if(pstmt!=null){
System.out.println(“enter the name to retreive:”);
name=sc.next();
pstmt.setString(1,name);
resultset=pstmt.executeQuery();
}
if(resultset!=null){
if(resultset.next()){
//fetching name from database
String name=resultset.getString(1);
//fetching image
inputStream is=resultSet.getBinaryStream(2);
File f=new File(“naturealphaimage.jpg”);
FileOutputStream fos=new FileOutputStream(f);
//copying from inputstream to output stream
byte [ ] buffer=new byte[1024];
while(is.read(buffer)>0){
fos.write(buffer);
}
fos.flush();
}
}
catch(SQLException s){
s.printStackTrace();
}catch(FileNotFoundException f){
f.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
finally{
if(con!=null){
con.close();
}if(pstmt!=null){
Pstmt.close();
}if(resultset!=null){
Resultset.close();
}
}
}
}