How I can store images with jdbi?

576 views
Skip to first unread message

Trossky Garcia

unread,
Aug 1, 2016, 1:04:23 AM8/1/16
to jDBI
I am working with mysql and dropwizard, I have a model with a field [Blob], I want store images there. But I  create Blob  with a array bytes and  i do the insertion with a DAO. Note:In my database, the column Blob is empty. And I can't  store my image.
Help me!. Thank!

Matthew Hall

unread,
Aug 1, 2016, 12:07:09 PM8/1/16
to jd...@googlegroups.com
Hi Trossky,

Can you give us an example of the code you're using to store the image? How are you passing the image data to JDBI?

On Sun, Jul 31, 2016 at 11:04 PM, Trossky Garcia <trossk...@gmail.com> wrote:
I am working with mysql and dropwizard, I have a model with a field [Blob], I want store images there. But I  create Blob  with a array bytes and  i do the insertion with a DAO. Note:In my database, the column Blob is empty. And I can't  store my image.
Help me!. Thank!

--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Trossky

unread,
Aug 2, 2016, 2:56:13 PM8/2/16
to jDBI
Hi  Matthew Hall,
My code: 

1. method:

public Either<IException,String > createAnnouncement(Integer id_Person, String createADTOString, InputStream imageByteArray) {

try {

byte[] image = convertImagetoByte(imageByteArray);
Blob blob=new SerialBlob(image);

long id_announcement = announcementDAO.createAnnouncementTest(createADTO.getStart(), createADTO.getEnd(), createADTO.getType(), image);

} catch (Exception e) {

return Either.left(ManagementException.catchException(e));
}
}


2. JDBI

@SqlUpdate("INSERT INTO announcement (start,end,type, image) VALUES(:start, :end,:type, :imageAux)")
@GetGeneratedKeys
long createAnnouncementTest(@Bind("start")Timestamp start, @Bind("end")Timestamp end,@Bind("type") String type, @Bind("imageAux") byte[] blob);

--

I try to pass the image field as a byte or blob. But in my bd the image , the record is undefined






Steven Schlansker

unread,
Aug 3, 2016, 3:35:05 PM8/3/16
to jd...@googlegroups.com
It looks like you are creating a Blob object but then not using it in your insert?
Shouldn't the Blob be one of the parameters to createAnnouncement?
signature.asc

Trossky

unread,
Aug 3, 2016, 3:49:44 PM8/3/16
to jDBI

I tested the BLOB object as a parameter, and the Byte. but none work.
For example:

@SqlUpdate("INSERT INTO announcement (start,end,type, image) VALUES(:start, :end,:type, :imageAux)")
@GetGeneratedKeys
long createAnnouncementTest(@Bind("start")Timestamp start, @Bind("end")Timestamp end, @Bind("type") String type, @Bind("imageAux") Blob blob);

Steven Schlansker

unread,
Aug 3, 2016, 6:20:41 PM8/3/16
to jd...@googlegroups.com
Did it just silently fail, or was there some sort of exception?
Did MySQL log any warnings? Sometimes it has a nasty tendency to ignore
error conditions and helpfully do nothing instead.

Does inserting the blob directly using the JDBC API work?
signature.asc
Reply all
Reply to author
Forward
0 new messages