File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Using Eclipse SWT Image, JDBC SQLite and JAVA to insert, store and retrieve Images. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Using Eclipse SWT Image, JDBC SQLite and JAVA to insert, store and retrieve Images." Watch "Using Eclipse SWT Image, JDBC SQLite and JAVA to insert, store and retrieve Images." New topic
Author

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

shay main
Greenhorn

Joined: Oct 28, 2010
Posts: 6
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

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
shay main
Greenhorn

Joined: Oct 28, 2010
Posts: 6
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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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

Joined: Oct 28, 2010
Posts: 6
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

Joined: Oct 28, 2010
Posts: 6
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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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

Joined: Oct 28, 2010
Posts: 6
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Eclipse SWT Image, JDBC SQLite and JAVA to insert, store and retrieve Images.