Sample demonstrating usage of BFiles

Table Of Contents 

Overview of the Sample Application 

Back To Top

This sample illustrates accessing of BFiles using JDBC. This sample uses the JDBC-Thin driver; the code is the same for JDBC-OCI, except for the database URL syntax.

Working of the Sample

  • When the application is invoked, database connection status will be displayed in the status bar. If the connection retrieval was successful, the application first drops the table BFILE_TABLE if it already exists. Then it creates a new table BFILE_TABLE(BFILE_COLUMN BFILE).
  • After creating the table, the application prompts the user to Enter Full Directory Path of the image file (architect.gif). When the user enters the correct path, it inserts a single row comprising of the image file 'architect.gif' into the table.

  • Note: 'architect.gif' file is provided along with the sample. It has to reside in the server in which database is running

  • After insertion of BFile Data, user can press "SELECT" to view the image in the panel.
  • Pressing of "SELECT" button will trigger the following events:
  • BFile Locator is selected from the table BFILE_TABLE
  • Binary Stream is opened on the selected BFILE locator
  • Image Data (Binary Data) is fetched
  • Image is displayed

Here is the code usage for reading from a BFILE table. You can find more details of the code in BFileSample.java file under src/oracle/otnsamples/jdbc/bfile folder. Look into Description of Sample Files section for folder and file details.

private void selectBFILE() {
.......................
.......................

// Prepare a Statement
Statement stmt = connection.createStatement();

// Execute the Query and get BFILE Locator as a result set
ResultSet rset = stmt.executeQuery("SELECT * FROM BFILE_TABLE ");

// Note : Using Oracle Extension oracle.sql.BFILE to get BFILE Locator
oracle.sql.BFILE bfile = null;

.......................
.......................

bfile = ((OracleResultSet)rset).getBFILE(1); // Get the BFILE Locator

.......................

bfile.openFile();

// Open the Input Binary Stream with getBinaryStream method of
// oracle.sql.BFILE class
InputStream instream = bfile.getBinaryStream();

......................
......................

Required Software

Back To Top
  • Oracle9i JDeveloper ( Note: Oracle9i JDeveloper is Oracle's Visual Java Development Tool and can be downloaded from here )
    or
    JDK1.2.x or above This can be downloaded from here .
  • Oracle9i Database or higher running SQL*Net TCP/IP listener. This can be downloaded from here .
  • Oracle9i JDBC Driver. The JDBC driver is available at ORACLE_HOME/jdbc/lib. Or it could be downloaded from here.

Application Set-up and Configuration

Back To Top
  • Unjar the provided BFileSample.jar using the following command 

  • > jar xvf BFileSample.jar

    Note: You will find jar.exe in JDK_HOME\bin. Ensure JDK_HOME\bin is present in your system path. 
    (JDK_HOME is the root directory of the JDKx.x installation). This creates a folder
    BFileSample with all the source files

  • Go to BFileSample Directory. You will find the image file 'architect.gif'. You need to ftp/copy this .gif file on to the Server where Database is mounted. Please note the complete path of the directory where you have copied this image file. This is the directory that you will need to specify when the application prompts for the full path to the image on the database server.

  • Ensure that the database user you are using for this samples has CREATE ANY DIRECTORY system privileges. This can be done by running the following command at the SQL prompt after connecting to database as system/<password>

    SQL> GRANT CREATE ANY DIRECTORY TO <database-username>;

  • Edit BFileSample/Connection.properties file in your favorite editor. Change the HostName, Port, SID, UserName and 
  • Password to connect to your own database.
HostName = localhost
SID = ORCL
Port = 1521
UserName = scott
Password = tiger

Running the Application 

Back To Top

This sample application can be run in 3 different ways listed below.

From Oracle9i JDeveloper

    • Open Oracle9i JDeveloper and use File/Open option to select the BFileSample.jws from the BFileSample directory.
    • Next, select Project/Make BFileSample.jpr from main menu.
    • Now, select Run/Run BFileSample.jpr from main menu to run the application.

From JDK for Windows

This section will describe steps to run the application from console using JDK on Windows. The sample can be run either manually or using a script file .

Run application using batch File: run.bat provided:

By setting few environment variables, the sample application could be directly run by just executing the batch file: run.bat from the command prompt, from BFileSample directory. Environmental variables JAVA_HOME and JDBC_HOME have to be set before running run.bat file.
Example:

D:\BFileSample> set JDBC_HOME=d:\oracle9i\jdbc\lib
D:\
BFileSample> set JAVA_HOME=d:\jdk1.3.1
D:\
BFileSample> run

Running the application manually:

  • Set CLASSPATH to include Oracle9i JDBC Driver file: classes12.zip or classes12.jar. 
  • Also add BFileSample directory where Connection.properties exists and the current directory to the CLASSPATH
    Example:
     
    D:\BFileSample>set CLASSPATH=D:\oracle9i\jdbc\lib\classes12.zip;D:\BFileSample;.
  • From the directory BFileSample\src\oracle\otnsamples\jdbc\bfile, compile all the java files using javac:
    Example:
     
    D:\BFileSample\src\oracle\otnsamples\jdbc\bfile>javac -d . *.java
  • Run the class file using java from  BFileSample\src\oracle\otnsamples\jdbc\bfile directory
    Example:
    D:\BFileSample\src\oracle\otnsamples\jdbc\bfile>
    java oracle.otnsamples.jdbc.bfile.BFileSample

From JDK for Red Hat Linux release 8.0

This section will describe steps to run the application from console using JDK on Linux. The sample can be run either manually or using a script file .

Run application using batch File: run.sh provided:

By setting few environment variables, the sample application could be directly run by just executing the batch file: run.sh from the command prompt, from BFileSample directory. Environmental variables JAVA_HOME and JDBC_HOME have to be set, else the user will be prompted to enter values.

  • Go to BFileSample directory and from the $ prompt use the command below to give execute permission to the file.
    $chmod 777 run.sh
  • Now run the file:
    $run.sh

Running the application manually:

  • Set CLASSPATH to include Oracle9i JDBC Driver file: classes12.zip or classes12.jar.
  • BFileSample  directory where Connection.properties exists and current directory are also added to the CLASSPATH.
    Example:
    $export CLASSPATH=/home1/jdbc/lib/classes12.zip:/home1/BFileSample:.
  • From the directory BFileSample/src/oracle/otnsamples/jdbc/bfile, compile all the java files using javac:
    Example:
    $javac -d . *.java
  • Run the class file using java from the same BFileSample/src/oracle/otnsamples/jdbc/bfile directory.
    Example:
    $java oracle.otnsamples.jdbc.bfile.BFileSample

Description of Sample Files 

Back To Top
The directory structure of the deliverable BFileSample.jar will be as shown below. BFileSample is the top level directory

Directory
Files
Description
BFileSample BFileSample.jws The Oracle9i JDeveloper workspace file
BFileSample.jpr The Oracle9i JDeveloper project file
Connection.properties This file has the details of the database connection parameters
run.bat The batch file to compile and run the sample in Windows environment.
run.sh The batch file(shell script) to compile and run the sample in Linux environment.
BFileSample\doc Readme.html This file
architect.gif Image file which is stored as BFILE Column
BFileSample\src\oracle\otnsamples\jdbc\bfile BFileSample.java The source file for sample
BFileFrame.java The source file for the sample User Interface


Please enter your comments about this sample in the OTN Sample Code Discussion Forum.
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy