-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to store a file in hibernate using blob
PostPosted: Tue May 15, 2007 3:57 am 
Newbie

Joined: Tue May 15, 2007 2:54 am
Posts: 4
I need help in inserting a file in MySQL.
I would be really greatful, if u could attach piece of code, for doing it.
Hope to get a reply soon.
Thanks in advance.


Top
 Profile  
 
 Post subject: Re: How to store a file in hibernate using blob
PostPosted: Thu May 17, 2007 1:50 am 
Newbie

Joined: Tue May 15, 2007 2:54 am
Posts: 4
I found the solution. Here i m sharing the code.

FileInputStream fis=new FileInputStream(file);
byte b[] = new byte[fis.available()];
fis.read(b);
java.sql.Blob bb = Hibernate.createBlob(b);
TblobForm tblob = new TblobForm();
tblob.setfiledata(bb);
tblob.setId(1);
fis.close();
session.save(tblob);

I have 2 field id and photo
id is of type integer
filedata is of type blob.

And to retrieve from the database:


TblobForm form = new TblobForm();
byte b[] = form.getFileData().getBytes(1,(int)form.getFileData().length());
java.io.FileOutputStream f = new java.io.FileOutputStream(filename);
f.write(b);


I hope this will help u all.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.