Oracle BLOB

by

In Oracle, the BLOB (Binary Large Object) data type is used to store large amounts of binary data, such as images, audio files, video files, and other unstructured binary information.

Here’s a detailed guide on using BLOB in Oracle:

1. Creating a Table with a BLOB Column

You can create a table that includes a BLOB column. The following is an example of creating a table named media_table to store media – related binary data:

-- Create a table with a BLOB column
CREATE TABLE media_table (
id NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,
file_name VARCHAR2(255),
file_content BLOB
);

2. Inserting Binary Data into a BLOB Column

There are multiple ways to insert binary data into a BLOB column. One common approach is to use the INSERT statement along with a BFILE or by using a programming language with JDBC.
Using BFILE

-- First, create a directory object to point to the file location
CREATE OR REPLACE DIRECTORY media_dir AS '/path/to/media/files';


-- Insert data using BFILE
DECLARE
v_file BFILE;
v_blob BLOB;
BEGIN
INSERT INTO media_table (file_name, file_content)
VALUES ('example.jpg', EMPTY_BLOB())
RETURNING file_content INTO v_blob;


v_file := BFILENAME('media_dir', 'example.jpg');
DBMS_LOB.OPEN(v_file, DBMS_LOB.LOB_READONLY);
DBMS_LOB.LOADFROMFILE(v_blob, v_file, DBMS_LOB.GETLENGTH(v_file));
DBMS_LOB.CLOSE(v_file);
COMMIT;
END;

 

Using Java with JDBC

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class InsertBlobExample {
    public static void main(String[] args) {
        try {
            // Establish a connection to the Oracle database
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@your_host:your_port:your_sid", "your_user", "your_password");
            String sql = "INSERT INTO media_table (file_name, file_content) VALUES (?,?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);

            // Set the file name
            pstmt.setString(1, "example.jpg");

            // Read the binary file
            File file = new File("example.jpg");
            InputStream inputStream = new FileInputStream(file);
            pstmt.setBinaryStream(2, inputStream, (int) file.length());

            // Execute the insert statement
            pstmt.executeUpdate();

            // Close resources
            inputStream.close();
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

You can use DBBlobEditor to Batch insert Oracle table BLOB data from files

3. Querying Binary Data from a BLOB Column

When querying binary data from a BLOB column, you can retrieve the data and save it to a file or process it in your application.
Using Java with JDBC

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class RetrieveBlobExample {
    public static void main(String[] args) {
        try {
            // Establish a connection to the Oracle database
            Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@your_host:your_port:your_sid", "your_user", "your_password");
            Statement stmt = conn.createStatement();
            String sql = "SELECT file_content FROM media_table WHERE id = 1";
            ResultSet rs = stmt.executeQuery(sql);

            if (rs.next()) {
                java.sql.Blob blob = rs.getBlob("file_content");
                byte[] bytes = blob.getBytes(1, (int) blob.length());

                // Save the binary data to a file
                OutputStream outputStream = new FileOutputStream("retrieved.jpg");
                outputStream.write(bytes);
                outputStream.close();
            }

            // Close resources
            rs.close();
            stmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

You can use DBBlobEditor to Batch export Oracle table BLOB data to files

Storage: Storing large BLOB data can consume a significant amount of disk space. You should carefully plan your storage strategy, and you might consider storing files on the file system and only keeping references in the database.
Performance: Retrieving and inserting large BLOB data can be slow. You may need to optimize your application and database configuration, such as adjusting buffer sizes and using appropriate I/O settings.

Here are some common use cases for storing binary data using the Oracle BLOB data type:

1. Storing Images

Images can be stored as Oracle BLOB data.

2. Audio and Video Storage

You can store the media (Audio and Video) files Oracle BLOB data.

3. Document Storage

You can store various types of documents such as PDFs, Word documents, and Excel spreadsheets Oracle BLOB data.

In conclusion, Oracle’s BLOB data type provides a means to manage large binary objects, but it’s crucial to be aware of storage and performance implications when using it.