Java BLOB
The two data types used in Java to store binary and big-character objects are BLOB and CLOB. In contrast to other types of data like float, int, double, etc., it is unique. All of them are referred to as LOB (Large Objects). First, we will talk about Java's BLOB type of data and Blob interface. In a Java programme, the BLOB datatype will also be used.
Datatype BLOB
Binary Large Objects is the acronym for a BLOB (BLOB). It is a built-in data type that represents a binary string with variable lengths that can be as long as 2,147,483,647 characters. As opposed to certain other binary types, the BLOB does not have a coding page connected with it. The character data is not stored in BLOB strings.
The BLOB's size is expressed in bytes.We employ the K, M, and G characters, which stand for multiples of 1024, 1024*1024, and 1024*1024*1024, respectively, to represent BLOB data types. Whenever a transaction is confirmed, a BLOB's life cycle comes to a close. If we wish to store really huge binary values, we should utilise the BLOB data type. Keeping pictures and other multimedia content in the system, for instance.
Java offers the getBlob() methods on the java.sql.ResultSet to obtain Blob-type objects.
Limitations of BLOB
- The LOB type is constrained by the following factors.
- For equality (=) and non-equality (!=, >), it is impossible to compare them.
- Since the numbers cannot be sorted, the tests =, >, and >= cannot be used.
- It can't be used as a critical column or in indexes.
- The phrases DISTINCT, GROUP BY, and ORDER BY cannot be used on a LOB-type. Because casting depends on the platform and database, the implicit casting of LOB-type is also forbidden.
Syntax:
[(length [K | M | G])] is a BLOB | BINARY LARGE OBJECT
If we don't provide the suffix (K, M, and G), it automatically picks G and takes up two gigabytes of space (2,147,483,647).
BLOB Interface
The java.sql package contains the Java Blob interface. This is where the vast binary value is kept. The java.sql.The blob interface is implemented with a LOCATOR. It indicates that rather than providing a copy of the object, the Blob implementation provides a logical pointer to a LOB (big thing). Java.sql.Types are mapped to SQL datatypes in Blob.
Blob Interface Methods
The interface specifies the ways to obtain a SQL BLOB value's length, materialise (make a real) a BLOB value on the client, and locate a pattern of bytes inside BLOB data.
The SQLException is thrown by all of the Blob interface's methods. Suppose the system's JDBC drive doesn't support any of the Blob interface's methods. In that case, we can also receive the SQLFeatureNotSupportedExceptionwhen none of the Blob interface's operations is supported by the system's JDBC drive.
The Blob interface's techniques are explained in the following tables.
Return Type | Method Signature | Implementation Notes |
bytes | getBytes(long pos,int,length) | Arguments are generated if pos 1, pos exceeds the duration, or pos is equal to zero. |
long | position (byte[] pattern, long start) | The BLOB value that this Blob object reflects has a byte location where the provided byte array structure starts, which can be found using the method. At the location start, the pattern-finding process starts. If the pattern is equal to null, if the |
long | position (Blob pattern, long start) | It finds the starting byte point in the BLOB value that is indicated by such a Blob object. Position start is where the search starts. If the patterns equal to null, if the start is less than 1, if the pattern has length 0, or if an exception is thrown when attempting to read the first byte of the patterns, an exception will be issued. |
void | free() | The Blob's properties are released via the technique. Whenever the free() method is used, an object is rendered null and void. |
InputStream | getBinaryStream in (long pos, long length) | This gives back an InputStream object with a partial Blob value, beginning with the byte indicated by pos and having a duration of bytes. |
InputStream | getBinaryStream() | The BLOB data is retrieved as streams. |
long | length() | That method computes how many bytes are in the BLOB data that this Blob object has specified. |
OutputStream | setBinaryStream (long pos) | In the process of writing to the BLOB information that this Blob object contains, it obtains a stream. Starting at location pos, the stream runs. |
int | setByte(long pos, byte[] bytes) | The technique updates the BLOB value with an array of bytes. Beginning at location pos, the Blob object represents. The amount of transferred bytes is returned. |
int | setBytes(long pos,byte[] bytes, int offset,intlen) | This procedure transfers the entire or a portion of the byte arrays to the Blob variable using the parameters long pos, byte[] bytes, int offset, and int len. The amount of bytes transferred is represented by and returned by this Blob object. |
null | truncate (long len) | The technique truncates the Blob value. |