Javatpoint Logo
Javatpoint Logo


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.

BLOB Datatype

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.

  • It cannot be compared for equality (=) and non-equality (!=, <>).
  • The values are not orderable, so <, <=, >, >= tests are not supported.
  • It cannot be used in indices or as primary key columns.
  • The clauses DISTINCT, GROUP BY, and ORDER BY cannot be applied on LOB-type.
  • Implicit casting of LOB-type is also prohibited because casting is platform and database dependent.


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.

Blob Interface

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.

Return Type Method Signature Implementation Notes
byte[] getBytes(long pos, int length) Exceptions are raised if pos < 1, if pos is larger than the length of the, or if length <= 0.
long position(byte[] pattern, long start) The method is used to retrieve the byte position at which the specified byte array pattern begins within the BLOB value that this Blob object represents. The search for pattern begins at position start.
Exceptions are raised if pattern == null, if start < 1, or if pattern is an array of length 0.
long position(Blob pattern, long start) It retrieves the byte position in the BLOB value designated by this Blob object at which the pattern begins. The search begins at position start.
Exceptions are raised if the pattern == null, if start < 1, if pattern has length 0, or if an exception is thrown when trying to read the first byte of the pattern.
void free() The method releases the resources that Blob holds. An object becomes invalid when the free() method is invoked.
InputStream getBinaryStream(long pos, long length) It returns an InputStream object that contains a partial Blob value, starting with the byte specified by pos, which is length bytes in length.
InputStream getBinaryStream() It retrieves the BLOB value as a stream.
long length() The method returns the number of bytes in the BLOB value designated by this Blob object.
OutputStream setBinaryStream(long pos) It retrieves a stream that can be used to write to the BLOB value that this Blob object represents. The stream begins at position pos.
int setByte(long pos, byte[] bytes) The method writes an array of bytes to the BLOB value. The Blob object represents, starting at position pos. It returns the number of bytes written.
int setByte(long pos, byte[] bytes, int offset, int len) The method writes all or partial part of the byte array to the Blob value. This Blob object represents and returns the number of bytes written.
void truncate(long len) The method truncates the Blob value.

Next TopicJava Calculate Age

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA