• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Binary Object to the database

 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Anybody having a sample coding on how to serialize the binary objects(BLOB) to the database and retrieve the same from the database. Is so, please give me that sample coding.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there are differences in JDBC drivers and you should check the documentation
for drivers you're using, but something like this should work
insert into database

int rows=0;
PreparedStatement pstmt = null;
try
{
pstmt = conn.prepareStatement("insert into PROVIDERS ( "+
" party_id, "+
"image ) "+
"values( "+
"?, "+
"?)");
pstmt.setInt(1,112);
File file = new File("pilo.png");
long fileLength = file.length();
FileInputStream fis = new FileInputStream(file);
pstmt.setBinaryStream(2,fis, (int)fileLength);
rows = pstmt.executeUpdate();
fis.close();
pstmt.close();
pstmt =null;
conn.commit();
}
catch (FileNotFoundException e) {
...
}
catch (IOException e) {
...
}
catch (SQLException e) {
...
}
finally {
if (stmt != null){ stmt.close();
catch Exception e) {
... }
}
}

to retrieve from database



[added code tags]
[ September 13, 2005: Message edited by: Jeanne Boyarsky ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic