In Java, BLOB and CLOB are the two data types used to store binary and character large objects, respectively. It is different from other data types like float, int, double, etc. Collectively it refers to as LOB (Large Objects).
In this section, we will discuss the BLOB datatype and Blob interface in Java. Also, we will use BLOB datatype in a Java program.
A BLOB stands for Binary Large Objects (BLOB). It is a built-in data type that represents a varying-length binary string that can be up to 2,147,483,647 characters long. Note that the BLOB is not associated with the code page like other binary types. BLOB string does not hold the character data.
The length of the BLOB is measured in bytes. To represent a BLOB data type, we use K, M, and G characters that represent the multiples of 1024, 1024*1024, 1024*1024*1024, respectively. The lifespan of a BLOB ends when a transaction commits. We should use the BLOB data type if we want to store very large binary values. For example, storing images and other multimedia files in the database.
In order to retrieve a Blob type object, Java provides the getBlob() method on the java.sql.ResultSet.
Restrictions on BLOB
There are the following restrictions on the LOB type.
If we do not specify the suffix (K, M, and G), it takes G by default and occupies a space of two gigabytes (2,147,483,647).
The above SQL query creates a table named Pictures and stores images in binary form. The size of the image should not be more than 16 MB.
Java Blob interface belongs to the java.sql package. It is used to store the large binary value. The implementation of the java.sql.Blob interface is LOCATOR-based. It means that the Blob implementation offers a logical pointer to a LOB (large object) rather than a copy of the object. In Blob, the java.sql.Types are mapped to the SQL datatypes.
Blob Interface Methods
The interface defines the methods for getting the length of an SQL BLOB value, for materializing (actual) a BLOB value on the client, and for determining the position of a pattern of bytes within a BLOB value. In addition, this interface has methods for updating a BLOB value. All methods of the Blob interface must be fully implemented if the JDBC driver supports the data type.
All the methods of the Blob interface throw the SQLException. We can also get the SQLFeatureNotSupportedException if the JDBC drive installed in the system does not support any of the methods of the Blob interface.
The following table describes the methods of the Blob interface.