BFILEs

There are times when data, such as reference data, is provided by an external vendor on read-only media. You can go through the work of creating a database schema to hold the data, create procedures to load the data into the database as BLOBs, and then repeat those procedures again and again and again, as the reference data is updated, but BFILEs provide a better solution to this problem. BFILEs allow you to simply go through the database and directly access the data on the host’s filesystem.

An instance of a oracle.sql.BFILE class is used in your Java program to hold a copy of a BFILE’s read-only locator from the database. You can then use the oracle.sql.BFILE object’s methods to retrieve the contents of the external binary file using streams.

To use BFILEs, you must follow these steps:

  1. Create a directory on the host’s filesystem for your files and store your files there.

  2. Create a directory object in the database to store the host filesystem’s directory specification using the create directory DDL statement.

  3. Create a table to hold BFILE locators for the external binary files.

  4. Insert locators for the external binary files into the table.

  5. Retrieve the external binary file data using the BFILE locator.

In the earlier section on BLOBs, we stored a photo in the database. In this section, instead of storing a photo in the database, we’ll store it in an operating-system file. To do this, create a BFILE directory, c:\TestBfile, on your host. Then create a directory object in the database ...

Get Java Programming with Oracle JDBC now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.