This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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)
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:
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.
Joined: Oct 28, 2010
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?
Joined: Oct 28, 2010
Nah, that didn't work....
Exception in thread "main" org.eclipse.swt.SWTException: Unsupported or unrecognized format