Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting Picture

 
Pradeep Selvaraj
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to use a BLOB. This can store anything as an array of bytes.
 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Chris Rosenquest
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Pradeep Selvaraj
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chris and Pradeep.I appreciate ur efforts.Thanks Again.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic