You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Manuel <ma...@ilch.de> on 2010/10/15 09:06:38 UTC

[DBUTILS] BeanHandler plus Blob

Hi,

have a class

public class Person {
  private Blob image;
  public void setImage(Blob image) { this.image = image; }
  public Blob getImage() { return image; }
}

Now I want to load the Person-class with BeanHandler-class
ResultSetHandler<Person> h = new BeanHandler<Person>(Person.class);
Person person = runner.query("select image from ...", h);

Works so far :-) Now what not works:

person.getImage();
ObjectInputStream ois = null;
ois = new ObjectInputStream(blob.getBinaryStream());
ImageIcon icon = (ImageIcon) ois.readObject();

-> Exception
You cannot invoke other java.sql.Clob/java.sql.Blob methods after calling
the free() method or after the Blob/Clob's transaction has been committed
or rolled back.

Do the query myself, or and write a own BeanHandler...
But shouldn't there a solution to just say
1.) write blob data in a stream x
2.) write blob data to an object x

OR ... go the other way, means change Person class to have an object let's
say instead Blob and ImageIcon and tell dbutils how to map from blob to
ImageIcon...

thanks for any response in advance ;)

greetings manuel


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Re: [DBUTILS] BeanHandler plus Blob

Posted by Manuel <ma...@ilch.de>.
Hi,

just if some else have similar problem... found the solution myself.

Overwrite BeanProcessor

public class MyBeanProcessor extends BeanProcessor {
  @Override
  rotected Object processColumn(ResultSet rs, int index, Class<?>
propType) throws SQLException {
    if (propType.equals(ImageIcon.class)) {
      return getImageIcon(rs.getBlob(index));
    }

    return super.processColumn(rs, index, propType);
  }
}

Create ResultSetHandler like that:

ResultSetHandler<Person> h = new BeanHandler<Person>(Person.class, new
BasicRowProcessor(new MyBeanProcessor()));

That would do the job, now you can map a blob to an ImageIcon.
You can implement other special type the same way!

Greetings Manuel


Am Fr, 15.10.2010, 09:06 schrieb Manuel:
> Hi,
>
> have a class
>
> public class Person {
>   private Blob image;
>   public void setImage(Blob image) { this.image = image; }
>   public Blob getImage() { return image; }
> }
>
> Now I want to load the Person-class with BeanHandler-class
> ResultSetHandler<Person> h = new BeanHandler<Person>(Person.class);
> Person person = runner.query("select image from ...", h);
>
> Works so far :-) Now what not works:
>
> person.getImage();
> ObjectInputStream ois = null;
> ois = new ObjectInputStream(blob.getBinaryStream());
> ImageIcon icon = (ImageIcon) ois.readObject();
>
> -> Exception
> You cannot invoke other java.sql.Clob/java.sql.Blob methods after calling
> the free() method or after the Blob/Clob's transaction has been committed
> or rolled back.
>
> Do the query myself, or and write a own BeanHandler...
> But shouldn't there a solution to just say
> 1.) write blob data in a stream x
> 2.) write blob data to an object x
>
> OR ... go the other way, means change Person class to have an object let's
> say instead Blob and ImageIcon and tell dbutils how to map from blob to
> ImageIcon...
>
> thanks for any response in advance ;)
>
> greetings manuel
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org