Call now: 252-767-6166  
Oracle Training Oracle Support Development Oracle Apps

 
 Home
 E-mail Us
 Oracle Articles


 Oracle Training
 Oracle Tips

 Oracle Forum
 Class Catalog


 Remote DBA
 Oracle Tuning
 Emergency 911
 RAC Support
 Apps Support
 Analysis
 Design
 Implementation
 Oracle Support


 SQL Tuning
 Security

 Oracle UNIX
 Oracle Linux
 Monitoring
 Remote s
upport
 Remote plans
 Remote
services
 Application Server

 Applications
 Oracle Forms
 Oracle Portal
 App Upgrades
 SQL Server
 Oracle Concepts
 Software Support

 Remote S
upport  
 Development  

 Implementation


 Consulting Staff
 Consulting Prices
 Help Wanted!

 


 Oracle Posters
 Oracle Books

 Oracle Scripts
 Ion
 Excel-DB  

Don Burleson Blog 


 

 

 


 

 

 
 

How to insert a BLOB image from a client PC

Oracle Tips by Burleson Consulting
July 18,  2008

Question:  I want to load a photograph image from my client PC into an Oracle table.  I know that utl_file cannot read files on a client PC, but I need to transfer the photograph BLOB image into Oracle.  What are the options for loading images from a PC into a table on a remote server?

Answer:  This is a very common function and each programmer will approach it differently.  Remember, the Oracle SQL*Net "client" is disconnected from direct access to the Oracle server, and the server can only communicate to the client as you define it.

For immediate expert assistance in Java graphics for images in Oracle, just call us or e-mail  .

 

A risky approach to moving images into Oracle

This forum thread shows PL/SQL stored procedures for loading images from a client side application, but there remains the issue of moving the BLOB from the remote client to the server, where PL/SQL can get it.  If you want to store the BLOB image on the server, you must write code to either "push" or "pull" it, not a good approach:

  • RISKY - PUSH BLOB - Use an FTP program to transfer the BLOB to a known directory on the server. After FTP is complete, invoke PL/SQL to load it.
     
  • RISKY - PULL BLOB- Have the server open a secure connection to the client disk as a shared device. Once the client-side disk is mounted, PL/SQL can reach it.
     
  • Use TNS - The first two approaches are risky and create unnecessary exposures.  By storing an empty BLOB table column, and then re-selecting it FOR UPDATE, you can transfer the LOB image from the PC into Oracle without opening any external connections.
     

The best approach for moving images from a PC to Oracle

As we note, it is easy to move-in a photograph of other LOB image, by storing an empty BLOB table column, and then re-selecting it FOR UPDATE, streaming the image into the BLOB column, and then storing it with a COMMIT.

To load images from a client PC, a programmer often uses Perl interfaces for BLOB's or Java OCI programs for loading LOB's.  Regardless of the language chosen, a basic method for storing BLOB's from a PC device (such as a PC camera) include these steps.

1. Get a connection
2. Create a statement object.
3. Insert an empty row in the table.
4. Select the temp blob back out of the table "FOR UPDATE".
5. Stream the blob content into the temp blob.
6. Commit.

Below is some psuedocode to save an image from a PC client onto a pictures table in Oracle, with the image table having only a primary key and a blob column.  If you don't want to write your own BLOB loading program, you can hire experts to create a custom program to capture and upload images from a client PC into an Oracle table, just contact us at  .

public boolean saveImageToDatabase(Image image, int pri_key)
{
if (!createEmptyBlobRow(pri_key)) return false;
BLOB tempBlob = getTempBlob(pri_key);
if (tempBlob == null) return false;
if (!writeBlobToDatebase(tempBlob)) return false;
return true;
}


private boolean createEmptyBlobRow(int pKey)
{
try
{
stat.executeUpdate("insert into x_pictures" +
call BC for complete script
" ( " + pKey + "," + EMPTY_BLOB()");
return true;
}
catch (SQLException e)
{
e.printStackTrace();
parm.logFile(e.toString());
}
return false;
}

private BLOB getTempBlob(int pKey)
{
try
{
rs = stat.executeQuery("select blob_content from x_pictures" +
call BC for complete script
if (rs.next()) return ((OracleResultSet) rs).getBLOB("blob_content");
}
catch (SQLException e)
{
e.printStackTrace();
parm.logFile(e.toString());
}
return null;
}



private boolean writeBlobToDatebase(BLOB t_Blob)
{
try
{
int chunksize = t_Blob.getChunkSize();
byte[] byteBuffer = new byte[chunksize];
call BC for complete script
long position = 1;
int bytesRead;
while ((bytesRead = b_in.read(byteBuffer)) != -1)
{
t_Blob.putBytes(position,byteBuffer);
position += bytesRead;
}
stat.execute("COMMIT");
return true;
}
catch (SQLException e)
{
e.printStackTrace();
parm.logFile(e.toString());
return false;
}
catch (FileNotFoundException e)
{
e.printStackTrace();
parm.logFile(e.toString());
return false;
}
catch (IOException e)
{
e.printStackTrace();
parm.logFile(e.toString());
return false;
}
}

}

 

 
Cosimo shares this example of using Coldfusion code that uses java to send a stream as a parameter to a stored procedure to store a Oracle BLOB.  It is capable of loading a record of several fields including a CLOB in 1 call to the database.  So I created a stored procedure :

create or replace PROCEDURE ADD_TEST_bLOB
( blobid IN VARCHAR2
, blobdata IN BLOB
) AS
begin
INSERT INTO test_blob_table values(blobid, blobdata);
COMMIT;
END ADD_TEST_BLOB;

and I call it from this coldfusion code :
<!---this handles a binary file for a blob--->

<!---build the db connection--->
<cfset factory = createobject("java","coldfusion.server.ServiceFactory")>
<cfset dsService = factory.getDataSourceService()>
<cfset ds = dsService.getDatasource( "DataSource1" )><!---this is the named datasource from the cf server--->
<cfset con = ds.getConnection()><!---This is the database connection for java objects to use.--->

<!---get the file and read it into a variable--->
<cfhttp url="http://server.com/test2.pdf" getAsBinary="yes" result="myfile"></cfhttp>

<!---create the stream for the blob--->
<cfset bytestreamobj = createobject("java","java.io.ByteArrayInputStream")>
<cfset bytestream = bytestreamobj.init(myfile.filecontent)>

<!---create the callable statement--->
<cfset cs = con.prepareCall("begin db.add_test_blob(?,?); end;")>

<!---assign the parameters and execute the statement--->
<cfscript>
cs.setObject("blobid","id");
cs.setBinaryStream("blobdata",bytestream,len(myfile.filecontent));
cs.execute();
</cfscript>

 


 

 

��
 
 
 
 

 
 
 

 
Oracle performance tuning software 
 
oracle dba poster
Oracle Linux poster
 
 

 

Burleson is the American Team

Note: This Oracle documentation was created as a support and Oracle training reference for use by our DBA performance tuning consulting professionals.  Feel free to ask questions on our Oracle forum.

Verify experience! Anyone considering using the services of an Oracle support expert should independently investigate their credentials and experience, and not rely on advertisements and self-proclaimed expertise. All legitimate Oracle experts publish their Oracle qualifications.

Errata?  Oracle technology is changing and we strive to update our BC Oracle support information.  If you find an error or have a suggestion for improving our content, we would appreciate your feedback.  Just  e-mail:  and include the URL for the page.


                    









Burleson Consulting

The Oracle of Database Support

Oracle Performance Tuning

Remote DBA Services


 

Copyright ? 1996 -  2014

All rights reserved by Burleson

Oracle ? is the registered trademark of Oracle Corporation.