mysql - Vaadin upload image and store it to database -
i using vaadin upload component , far have managed upload image directory, , display in panel component after successfull uploaded. want after this, insert in database aswell. have table called show has name, date , image. in show class have tried have image byte array or blob.
column(name="image") private byte[] image; @lob @column(name="image") private blob image;
in upload succeded method want convert file byte array, , far have tried this:
file file = new file("c:\\users\\cristina_pc\\desktop\\" + event.getfilename()); byte[] bfile = new byte[(int) file.length()]; try { fileinputstream fileinputstream = new fileinputstream(file); fileinputstream.read(bfile); uip.uploadimage(bfile); fileinputstream.close(); } catch (exception e) { e.printstacktrace(); }
i tried this:
byte[] data = files.readallbytes(new file("c:\\users\\cristina_pc\\desktop\\" + event.getfilename()).topath()); uip.uploadimage(data);
uip uploadimagepresenter, tried transform byte array blob, or pass repository byte array
public void uploadimage(byte[] data) throws serialexception, sqlexception{ //blob blob = new javax.sql.rowset.serial.serialblob(data); showrepo.updateafterimage(show, data); // or (show, blob) }
in repository, in updateafterimage method have:
public void updateafterimage(show show, byte[] data) //or blob data { em.gettransaction().begin(); //em - entitymanager show.setimage(data); em.gettransaction().commit(); }
either blob or byte array, can't manage update existing show setting image , update in database (the cell remains null). no error me figure out going wrong. help/advice useful. thanks!
i have found solution. made work was:
em.gettransaction().begin(); em.find(show.class, show.getid()); show.setimage(data); em.merge(spectacol); em.gettransaction().commit();
in updateafterimage method in show repository.
Comments
Post a Comment