-->
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.  [ 3 posts ] 
Author Message
 Post subject: Not yet success save image
PostPosted: Wed Feb 04, 2009 5:45 am 
Newbie

Joined: Tue Jun 10, 2008 4:40 am
Posts: 13
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


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 6:02 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
private Clob photo1;


Hmm... an image is usually a binary object. Try changing this to a Blob.

Quote:
photo.setPhoto1((Clob) Hibernate.createBlob(in));


If you did the change to Blob, just remove the cast. Otherwise, use Hibernate.createClob() to create a Clob.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 6:08 am 
Regular
Regular

Joined: Fri Jan 30, 2009 10:10 am
Posts: 74
Location: London
Why are you trying to use Clob?

Try something along the lines of:

@Lob
@Column(name="PHOTO1")
private byte[] photo1;

Once you get that working, then come back to consider whether you need lazy-loading and how to make that work.

--
Stephen Souness


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.