18

I want to insert and select images from sql server in jdbc. I am confused whether BLOB and byte are the same thing or different. I have used Blob in my code and the application loads slow as it has to select the images stored in Blob and convert it pixel by pixel. I want to use byte array but I don't know whether they are same or different. My main aim is to load the image faster. Thank you

8
  • How would you store a byte[] in a database without using a BLOB (Binary Large OBject)? Commented Sep 8, 2014 at 10:55
  • 1
    BLOB (Binary Large OBject)`. It's SQL's way of storing byte arrays. Commented Sep 8, 2014 at 10:56
  • 2
    stackoverflow.com/questions/6662432/… - Better use a BLOB - byte array . ByteArrayInputStrean - ImageIO.read. That should not pose any problem. (Though I prefer my files on the file system.) Commented Sep 8, 2014 at 10:58
  • 1
    I want to know whether there is difference between byte and binary Commented Sep 8, 2014 at 11:37
  • A byte is eight bits of binary. In this case they are effectively the same. Commented Sep 8, 2014 at 11:41

2 Answers 2

15

Before going further, we may need to remember about basic concepts about bit, byte and binary, BLOB.

Bit: Abbreviation of binary digit. It is the smallest storage unit. Bits can take values of 0 or 1.

Byte: Second smallest storage which is commonly (nibble is not mentioned since it is not very common term) used. It includes eight bits.

Binary: Actually, it is a numbering scheme that each digit of a number can take a value of 0 or 1.

BLOB: Set of binary data stored in a database. Also, type of a column which stores binary data inside.

To sum up definitions: Binary format is a scheme that which include bits.

To make it more concrete, we can observe results with the code below.

import java.nio.ByteBuffer;

public class TestByteAndBinary{

     public static void main(String []args){
        String s = "test"; //a string, series of chars
        System.out.println(s);

        System.out.println();

        byte[] bytes = s.getBytes(); //since each char has a size of 1 byte, we will have an array which has 4 elements
        for(byte b : bytes){
            System.out.println(b);
        } 

        System.out.println();

        for(byte b : bytes){
            String c = String.format("%8s", Integer.toBinaryString(b)).replace(' ', '0'); //each element is printed in its binary format
            System.out.println(c);
        }
     }
}

Output:

$javac TestByteAndBinary.java
$java -Xmx128M -Xms16M TestByteAndBinary
test

116
101
115
116

01110100
01100101
01110011
01110100

Let's go back to the question: If you really want to store an image inside a database, you have to use the BLOB type.

BUT! It is not the best practice.

  • Because databases are designed to store data and filesystems are designed to store the files.

  • Reading image from disk is a simple thing. But reading an image from the database need more time to accomplished (querying data, transforming to an array and vice versa).

  • While an image is being read, it will cause the database to suffer from lower performance since it is not simple textual or numerical read.

  • An image file doesn't benefit from characteristical features of a database (like indexing)

At this point, it is best practice to store that image on a server and store its path on the database.

As far as I can see on enterprise level projects, images are very very rarely stored inside the database. And it is the situation that those images were needed to store encrypted since they were including very sensual data. According to my humble opinion, even in that situation, those data had not to be stored in a database.

Sign up to request clarification or add additional context in comments.

Comments

1

Blob simply means (Binary Large Object) and its the way database stores byte array.

hope this is simple and it answers your question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.