{"id":27498,"date":"2025-04-22T08:03:22","date_gmt":"2025-04-22T08:03:22","guid":{"rendered":"http:\/\/www.withdata.com\/blog\/?p=27498"},"modified":"2025-10-17T03:41:07","modified_gmt":"2025-10-17T03:41:07","slug":"oracle-blob","status":"publish","type":"post","link":"https:\/\/www.withdata.com\/blog\/blob\/oracle-blob.html","title":{"rendered":"Oracle BLOB"},"content":{"rendered":"<p>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.<\/p>\n<h2>Here&#8217;s a detailed guide on using BLOB in Oracle:<\/h2>\n<h3>1. Creating a Table with a BLOB Column<\/h3>\n<p>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 &#8211; related binary data:<br \/>\n<code><br \/>\n-- Create a table with a BLOB column<br \/>\nCREATE TABLE media_table (<br \/>\nid NUMBER GENERATED BY DEFAULT ON NULL AS IDENTITY PRIMARY KEY,<br \/>\nfile_name VARCHAR2(255),<br \/>\nfile_content BLOB<br \/>\n);<br \/>\n<\/code><\/p>\n<h3>2. Inserting Binary Data into a BLOB Column<\/h3>\n<p>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.<br \/>\nUsing BFILE<br \/>\n<code><br \/>\n-- First, create a directory object to point to the file location<br \/>\nCREATE OR REPLACE DIRECTORY media_dir AS '\/path\/to\/media\/files';<\/code><br \/>\n<code><br \/>\n-- Insert data using BFILE<br \/>\nDECLARE<br \/>\nv_file BFILE;<br \/>\nv_blob BLOB;<br \/>\nBEGIN<br \/>\nINSERT INTO media_table (file_name, file_content)<br \/>\nVALUES ('example.jpg', EMPTY_BLOB())<br \/>\nRETURNING file_content INTO v_blob;<\/code><br \/>\n<code><br \/>\nv_file := BFILENAME('media_dir', 'example.jpg');<br \/>\nDBMS_LOB.OPEN(v_file, DBMS_LOB.LOB_READONLY);<br \/>\nDBMS_LOB.LOADFROMFILE(v_blob, v_file, DBMS_LOB.GETLENGTH(v_file));<br \/>\nDBMS_LOB.CLOSE(v_file);<br \/>\nCOMMIT;<br \/>\nEND;<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Using Java with JDBC<\/p>\n<pre>import java.io.File;\r\nimport java.io.FileInputStream;\r\nimport java.io.InputStream;\r\nimport java.sql.Connection;\r\nimport java.sql.DriverManager;\r\nimport java.sql.PreparedStatement;\r\n\r\npublic class InsertBlobExample {\r\n    public static void main(String[] args) {\r\n        try {\r\n            \/\/ Establish a connection to the Oracle database\r\n            Connection conn = DriverManager.getConnection(\"jdbc:oracle:thin:@your_host:your_port:your_sid\", \"your_user\", \"your_password\");\r\n            String sql = \"INSERT INTO media_table (file_name, file_content) VALUES (?,?)\";\r\n            PreparedStatement pstmt = conn.prepareStatement(sql);\r\n\r\n            \/\/ Set the file name\r\n            pstmt.setString(1, \"example.jpg\");\r\n\r\n            \/\/ Read the binary file\r\n            File file = new File(\"example.jpg\");\r\n            InputStream inputStream = new FileInputStream(file);\r\n            pstmt.setBinaryStream(2, inputStream, (int) file.length());\r\n\r\n            \/\/ Execute the insert statement\r\n            pstmt.executeUpdate();\r\n\r\n            \/\/ Close resources\r\n            inputStream.close();\r\n            pstmt.close();\r\n            conn.close();\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>You can use <a href=\"https:\/\/www.withdata.com\/dbblobeditor\/\">DBBlobEditor <\/a>to <a href=\"\/blog\/oralobeditor\/how-to-batch-insert-oracle-blob-data-from-files.html\">Batch insert Oracle table BLOB data from files<\/a>.<\/p>\n<p>See also: <a href=\"\/blog\/blob\/oracle-insert-blob.html\">Oracle Insert BLOB<\/a>, <a href=\"\/blog\/blob\/oracle-update-blob.html\">Oracle Update BLOB<\/a>.<\/p>\n<h3>3. Querying Binary Data from a BLOB Column<\/h3>\n<p>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.<br \/>\nUsing Java with JDBC<\/p>\n<pre>import java.io.FileOutputStream;\r\nimport java.io.OutputStream;\r\nimport java.sql.Connection;\r\nimport java.sql.DriverManager;\r\nimport java.sql.ResultSet;\r\nimport java.sql.Statement;\r\n\r\npublic class RetrieveBlobExample {\r\n    public static void main(String[] args) {\r\n        try {\r\n            \/\/ Establish a connection to the Oracle database\r\n            Connection conn = DriverManager.getConnection(\"jdbc:oracle:thin:@your_host:your_port:your_sid\", \"your_user\", \"your_password\");\r\n            Statement stmt = conn.createStatement();\r\n            String sql = \"SELECT file_content FROM media_table WHERE id = 1\";\r\n            ResultSet rs = stmt.executeQuery(sql);\r\n\r\n            if (rs.next()) {\r\n                java.sql.Blob blob = rs.getBlob(\"file_content\");\r\n                byte[] bytes = blob.getBytes(1, (int) blob.length());\r\n\r\n                \/\/ Save the binary data to a file\r\n                OutputStream outputStream = new FileOutputStream(\"retrieved.jpg\");\r\n                outputStream.write(bytes);\r\n                outputStream.close();\r\n            }\r\n\r\n            \/\/ Close resources\r\n            rs.close();\r\n            stmt.close();\r\n            conn.close();\r\n        } catch (Exception e) {\r\n            e.printStackTrace();\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>You can use <a href=\"https:\/\/www.withdata.com\/dbblobeditor\/\">DBBlobEditor <\/a>to <a href=\"http:\/\/www.withdata.com\/blog\/oralobeditor\/how-to-batch-export-oracle-table-blob-data-to-files.html\">Batch export Oracle table BLOB data to files<\/a>.<\/p>\n<p><strong>Storage<\/strong>: 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.<br \/>\n<strong>Performance<\/strong>: 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.<\/p>\n<h2>Here are some common use cases for storing binary data using the Oracle BLOB data type:<\/h2>\n<h3>1. Storing Images<\/h3>\n<p><a href=\"http:\/\/www.withdata.com\/blog\/blob\/store-images-in-oracle-blob.html\">Images can be stored as Oracle BLOB data<\/a>.<\/p>\n<h3>2. Audio and Video Storage<\/h3>\n<p>You can store the media (<a href=\"https:\/\/www.withdata.com\/blog\/blob\/store-audio-files-in-oracle-blob.html\">Audio<\/a> and <a href=\"https:\/\/www.withdata.com\/blog\/blob\/store-video-files-in-oracle-blob.html\">Video<\/a>) files Oracle BLOB data.<\/p>\n<h3>3. Document Storage<\/h3>\n<p>You can store various types of documents such as <a href=\"https:\/\/www.withdata.com\/blog\/blob\/store-pdf-files-in-oracle-blob.html\">PDFs<\/a>, <a href=\"https:\/\/www.withdata.com\/blog\/blob\/store-word-documents-in-oracle-blob.html\">Word documents<\/a>, and <a href=\"https:\/\/www.withdata.com\/blog\/blob\/store-excel-spreadsheets-in-oracle-blob.html\">Excel spreadsheets<\/a> Oracle BLOB data.<\/p>\n<p>In conclusion, Oracle&#8217;s BLOB data type provides a means to manage large binary objects, but it&#8217;s crucial to be aware of storage and performance implications when using it.<\/p>\n<p>See also: <a href=\"\/tools\/oracle\/blob\">Some tools for Oracle BLOB<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4524],"tags":[97,1325],"class_list":["post-27498","post","type-post","status-publish","format-standard","hentry","category-blob","tag-blob","tag-oracle"],"_links":{"self":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/27498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/comments?post=27498"}],"version-history":[{"count":8,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/27498\/revisions"}],"predecessor-version":[{"id":40206,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/posts\/27498\/revisions\/40206"}],"wp:attachment":[{"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/media?parent=27498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/categories?post=27498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withdata.com\/blog\/wp-json\/wp\/v2\/tags?post=27498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}