jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases and the fly likes Inserting Picture Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Inserting Picture" Watch "Inserting Picture" New topic

Inserting Picture

Pradeep Selvaraj

Joined: Sep 29, 2003
Posts: 17
Hi there!
I need to insert a picture into ORACLE database using JDBC.I need to learn a way to do that.How to store pictures in ORACLE.All ur suggestions are welcome.
Thanks for ur time!
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32625

You should be able to use a BLOB. This can store anything as an array of bytes.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
In oracle there is a data type wich the data rangs is(if i am not wrong) 4GB. Those are called 'Large objects'. By having a table which consists of these type of object you can have pictures in the orcale database. I think you can use the 'PrepapredStatement' to do this. Write the sql statement you want for example,
Connection con // your connection.
PrepapredStatement ps = con.prepareStatement("INSERT INTO ..... VALUES (?,?));
ps.setObject(1,your picture object);
Hope this is ok and correct.

SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927


Chris Rosenquest

Joined: Aug 30, 2003
Posts: 11
You need to, as stated above, use a LOB to store the image into your DB.
Let us know if you get this sorted out.
Try this untested code:
private void storeSQL(String base64EncodedImage) {
Connection con = null;
PreparedStatement sqlCreate = null;
PreparedStatement sqlPopulate = null;
OracleResultSet set = null;
try {
con = connectionFromYourConnectionPool;
// Prepare a statement to insert and empty LOB in the DB.
// We need to have a LOB in the DB prior to writing to it.
sqlCreate = con.prepareStatement("INSERT into your_table (encodedImage) VALUES (EMPTY_BLOB())");

// Real CLOB data will be inserted later in this method)
// Prepare a statement to populate the BLOB we just stored in the DB.
sqlPopulate = con.prepareStatement("SELECT base64EncodedImage FROM your_table FOR update");
// Obtain the result set from the DB as an OracleResultSet so
// we can perform Oracle specific actions on the results
set = (OracleResultSet)sqlPopulate.executeQuery();

oracle.sql.BLOB blob = null;
if (set.next()) {
blob = set.getBLOB("base64EncodedImage");
OutputStream out = blob.getBinaryOutputStream();
// Write the image BLOB
} catch (SQLException e) {
// Handle exceptions
} finally {
// Cleanup resources
} // storeSQL

"When you feel like a third wheel. Become a unicycle."
Pradeep Selvaraj

Joined: Sep 29, 2003
Posts: 17
Thanks Chris and Pradeep.I appreciate ur efforts.Thanks Again.
I agree. Here's the link: http://aspose.com/file-tools
subject: Inserting Picture
jQuery in Action, 3rd edition