wood burning stoves 2.0*
The moose likes JDBC and the fly likes Inserting Picture Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Inserting Picture" Watch "Inserting Picture" New topic
Author

Inserting Picture

Pradeep Selvaraj
Greenhorn

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!
Cheers!
Pradeep.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30356
    
150

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


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hello,
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: 8919

http://otn.oracle.com/sample_code/tech/java/sqlj_jdbc/files/advanced/LOBSample/Readme.html


Groovy
Chris Rosenquest
Greenhorn

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.
Chris
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)
sqlCreate.execute();
// 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
out.write(base64EncodedImage.getBytes());
out.close();
} catch (SQLException e) {
// Handle exceptions
} finally {
// Cleanup resources
}
} // storeSQL


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

Joined: Sep 29, 2003
Posts: 17
Thanks Chris and Pradeep.I appreciate ur efforts.Thanks Again.
 
 
subject: Inserting Picture