How to save image to database, i had try like this, still not sucess.
thanks for your help.
Code:
import java.sql.Clob;
@Lob
@Column(name="PHOTO1", length=Integer.MAX_VALUE - 1)
@Basic(fetch=FetchType.LAZY)
private Clob photo1;
for get image
Code:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(JIFEmployeeCv.this);
if (returnVal == fc.APPROVE_OPTION ) {
File file = fc.getSelectedFile();
txtPathPict1.setText(file.getPath());
ImageIcon pic = new ImageIcon(txtPathPict1.getText());
btnPict1.setIcon(pic);
}
for save image
Code:
InputStream in= new BufferedInputStream(new FileInputStream(txtPathPict1.getText()));
byte b[]=new byte[in.available()];
int i=-1;
while((i=in.read())!=-1){
in.read(b);
System.out.println("Get byte[]: "+i);
}
photo.setPhoto1((Clob) Hibernate.createBlob(in));
session.save(photo);
got error
java.lang.ClassCastException: org.hibernate.lob.SerializableBlob cannot be cast to java.sql.Clob