How to convert BLOB to Byte Array in java

By Saruque Ahamed Mollick

In this Java tutorial, we are going to learn how to convert BLOB to Byte Array in Java. As this is CodeSpeedy so we will always go for the easiest and best understandable method to achieve our goal. In order to achieve BLOB to byte array conversion in Java, we need to use getBytes() method.

Here we will explain everything assuming that you are using MySQL and You want to convert BLOB data type into a Byte array in Java.

BLOB can hold a variable amount of data

There are usually four types BLOB. Those are:

  1. TIMYBLOB
  2. BLOB
  3. MEDIUMBLOB
  4. LONGBLOB

No need to worry about these. The only difference between these is – The maximum amount of data that can be stored by each blob.



If you are still interested then you can learn more about BLOB from here: What is BLOB Data Type?

How to convert an image to byte array in Java

Convert BLOB to Byte Array in Java With an Easy Example

In MySQL blob has a method: getBytes()

This method Retrieves all or part of the BLOB value that this Blob object represents, as an array of bytes.

Blob myblob= my_result_set.getBlob("Database_Field");
int myblobLength = (int) myblob.length();  
byte[] myblobAsBytes = myblob.getBytes(1, myblobLength);
blob.free();

( ResultSet name is my_result_set )

Don’t forget to put blob.free()

free() method frees the blob object and free up the resources that was hold by it.

I want to give you an another easy example so that you can get a idea on how to implement it.

Assume that you are using these:

  • MySQL
  • JDBC
  • An image
import java.awt.Image;
import java.awt.image.*;
import java.sql.*;
import java.util.*;

public class MyClass {
   public static void main(String args[]) throws Exception {
      Image my_custom_image = new BufferedImage(300,400, BufferedImage.TYPE_INT_RGB);
      String driver= "com.mysql.jdbc.Driver";
      String url= "jdbc:mysql://localhost/mydb";
      String username= "root";
      String password= "password";
      Connection conn = null;
      Statement stmt = null;
      Class.forName(driver);
      System.out.println("Connecting to the database...");
      conn = DriverManager.getConnection(url, username, password);
      System.out.println("Connection is OK!...");
      System.out.println("getting blob.......");
      stmt = conn.createStatement();
      String query = "SELECT * FROM anything_you_want";
      ResultSet my_result_set= stmt.executeQuery(query);
      while(my_result_set.next()) {
         Blob my_blob = my_result_set.getBlob("my_custom_image ");
         byte [] bytes = my_blob.getBytes(1l, (int)my_blob.length());
         for(int i=0; i<bytes.length;i++) {
            System.out.println(Arrays.toString(bytes));
         }
     }
   }
}

This will work fine.

Stay Connected with us for more Java Tutorial.

You may also read,

Guess The Number Game Using Java with Source Code

How to convert Byte Array to Image in java with easy example

Leave a Reply

Your email address will not be published. Required fields are marked *