Hi everyone:
Had anyone saved picture to database using Hibernate successfully?
Can hibernate proccess Blob data type ? I have a table that has a Blob field.(I am using MySQL). It can't save the image if I use the code :
Code:
PrintWriter pw=null;
resp.setContentType("text/html;charset=gb2312");
InputStream in=req.getInputStream();
byte b[]=new byte[in.available()];
int i=-1;
while((i=in.read())!=-1){
in.read(b);
System.out.println("Get byte[]: "+i);
}
try{
Session _session=HibernateUtil.currentSession();
Transaction t=_session.beginTransaction();
Buildingpicture bp=new Buildingpicture();
bp.setPicture_memo("qijiashe upload");
bp.setPicture_name("Lyopic");
System.out.println("Get Picture : "+b.toString());
bp.setPicture_file(Hibernate.createBlob(in));
_session.save(bp);
_session.flush();
t.commit();
HibernateUtil.closeSession();
in.close();
pw=resp.getWriter();
pw.println("Sucess...");
It is a Servlet. I want to save a user's upload picture to database. But the picture can't be saved to MySQL. I can get the inputStream indeed.
Is there any error in my code?
Thks!