views:

301

answers:

6

For this university assignment I have a Prize object which contains either text, Image, Video. I would like to save this information into a Blob field in a database which will be running on a PDA on Apache Derby. How can i add this data to the database?

Thanks in advanced.

A: 

Try to google for "serialization".

Joonas Pulakka
+1  A: 

I assume you'll be connecting via JDBC. If so, simply write your SQL and look at the setBlob method of a PreparedStatement. Should be pretty straightforward.

Nick
A: 

Assuming that you'll be using JDBC to talk to Apache Derby, you may want to look into the PreparedStatement of java.sql.

Hint: There is a function called setObject which allows you to set a parameter of a SQL statement to be a Object. There are also different set methods that allow you to set different types for the parameters.

The JDBC Database Access trail of The Java Tutorials may be helpful for getting more information on JDBC.

coobird
setObject does not necessarily handle any object.
Tom Hawtin - tackline
+4  A: 

In this article Five Steps to Managing Unstructured Data with Derby you can read how to do this.

It describes how to insert binary data into a column with the BLOB datatype in Apache Derby using JDBC.

Edwin
A: 

If you are using Netbeans (I assume Eclipse has similar functionality) you can setup your database schema and the create new Java entity classes from the database and it will generate the appropriate JPA classes for you.

http://hendrosteven.wordpress.com/2008/03/06/simple-jpa-application-with-netbeans/

This is nice as it allows you to focus on your code rather than the database glue code.

TofuBeer
A: 

Serialization is the easy way to do it, however if possible you could make it look like a real database table with a structure containing id (bigint), datatype (smallint), creationdate (date) and data (blob) and specifically make the client code to save the object's data there. This way you could do searches like "get all video prizes created between January 1st 2008 and January 15th 2009" and it wouldn't break down old data if your class would change too much for the serialization to stop working.

This sort of solution would be easy to extend in the future too if there would be need for it; I understand this is a school assignment and such need most likely won't ever surface but if your teacher/professor knows his stuff, I bet he's willing to give an extra point or two for doing this excercise in this way since it takes a bit more time and shows that you can take the steps to prepare in advance for coping in the everchanging landscape of software development.

Esko