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
-
How would you store a `byte[]` in a database _without_ using a BLOB (Binary Large OBject)? – Boris the Spider Sep 08 '14 at 10:55
-
1`BLOB` (Binary Large OBject)`. It's SQL's way of storing byte arrays. – Buhake Sindi Sep 08 '14 at 10:56
-
2http://stackoverflow.com/questions/6662432/easiest-way-to-convert-a-blob-into-a-byte-array - Better use a BLOB - byte array . ByteArrayInputStrean - ImageIO.read. That should not pose any problem. (Though I prefer my files on the file system.) – Joop Eggen Sep 08 '14 at 10:58
-
1I want to know whether there is difference between byte and binary – viper Sep 08 '14 at 11:37
-
A byte is eight bits of binary. In this case they are effectively the same. – rossum Sep 08 '14 at 11:41
-
Please share the code you have so far to store/load the image. – Wagner DosAnjos Sep 08 '14 at 12:28
-
Have you tried to measure time of selecting/inserting images stored in different formats? – Dmytro Plekhotkin Sep 08 '14 at 13:27
-
Storing images in a relational database is a bad idea. File storage is a much better solution and store the paths/pointers to the images in SQL. – Sherman Jun 17 '19 at 15:52
2 Answers
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.

- 551
- 6
- 19
Blob simply means (Binary Large Object) and its the way database stores byte array.
hope this is simple and it answers your question.