• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Using Eclipse SWT Image, JDBC SQLite and JAVA to insert, store and retrieve Images.

 
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all,

I'm not sure if this should be in the SWT section or the JDBC section.

I'm writing a basic Java application that allows a user to insert details about individuals into an SQLite database. I'm using Eclipse SWT for the GUI.

Eclipse SWT defines a type Image (org.eclipse.swt.graphics.Image) for displaying Images in a GUI.

I am trying to allow a user to browse the file system, select an image and then insert that image into a database. I also want to be able to retrieve that image from the database and display it in the GUI.

All pretty straightforward, but for the life of me I can't get it to work!! I've searched around a lot too and can't seem to find a solution to this.

I'll attach my code below, and I'm using:
Eclipse IDE for Java Developers (3.6)
sqlite-jdbc-3.7.2.jar
jdk1.6.0_22

Thanks in advance!

Shay

 
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have a lot of experience with SWT, but after browsing the Javadocs I think I found a (partial) solution.

First of all, you need a BLOB field in the database. The data type may be called differently (e.g. binary in SQL Server), but in general that's all BLOB - binary large object. Using JDBC you can use PreparedStatement's setBinaryStream, setBlob or setBytes method to set the data.

So all you then need to do is convert the Image into an InputStream or byte[]. And that's what the Javadocs helped me find for you.
If you take a look at class Image there is a method called getImageData(). This returns an ImageData instance. That has a public byte[] field called data. You can use that to store the image in the database.

Retrieval is a bit harder but still doable. From the ResultSet use getBinaryStream to get an InputStream. Use this to create a new ImageData instance. Then create a new Image using this ImageData instance and a Device instance. I'm sorry to say that's the bit where my (theoretical) solution falls short. I have no idea how to get an instance from that class.

Edit: sorry, I just re-read your code, and you have already solved the storing issue (and the getting the Device ). You have made one mistake: the ImageData instance returned by personImage.getImageData() is no longer connected to the Image:

Returns an ImageData based on the receiver Modifications made to this ImageData will not affect the Image.


So all you need to do (I think) is replace lines 154-156:
 
shay main
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help Rob.

Unfortunately, neither of the SQLite JDBC Drivers that I have tried have implemented the getBinaryStream() method. I get the following:

java.sql.SQLException: not implemented by SQLite JDBC driver

These are the two libraries I have used...

http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC - version 3.7.2
http://www.zentus.com/sqlitejdbc/ - version 056


 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations, you've found a non-JDBC compliant driver. ResultSet.getBinaryStream() should be present in all JDBC drivers.

Fortunately there is a workaround - use getBytes(), then use a ByteArrayInputStream. If getBytes() is also not supported you should check if BLOB is supported at all.
 
shay main
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trust me to find one

I tried to use getBlob() and setBlob() previously and they aren't supported either.

So you reckon I need to do the following?

 
shay main
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nah, that didn't work....

Exception in thread "main" org.eclipse.swt.SWTException: Unsupported or unrecognized format
at org.eclipse.swt.SWT.error(SWT.java:4083)
at org.eclipse.swt.SWT.error(SWT.java:3998)
at org.eclipse.swt.SWT.error(SWT.java:3969)
at org.eclipse.swt.internal.image.FileFormat.load(FileFormat.java:82)
at org.eclipse.swt.graphics.ImageLoader.load(ImageLoader.java:130)
at org.eclipse.swt.graphics.ImageDataLoader.load(ImageDataLoader.java:22)
at org.eclipse.swt.graphics.ImageData.<init>(ImageData.java:331)
at org.eclipse.swt.graphics.Image.<init>(Image.java:545)
 
Rob Spoor
Sheriff
Posts: 22821
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happens if you try the following:
If this works then the data from the database has become corrupted, or the image type is unsupported on this machine but not on the source machine.
 
shay main
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was getting pretty tight on time so I ended up giving up and just storing the images in a local folder rather than the database ;-(

I'll go back and try and get it working when I have some spare time. Thanks for all your help Rob.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic