• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Binary Object to the database

 
Mark Henryson
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • 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.
 
myriah wind
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic