Subject Re: How2 save javaobject in Firebird ?
Author rwilcom
Read/Write them as a byte[] to the database - use the following
methods to serialize/deserialize you objects. The objects must
implement serializable, and for best results with deep and heavy
objects don't rely on Java's serialization, write your own with
readExternal/writeExternal.

/**
* Class which converts objects into byte[] arrays and byte[] into
* objects - caller must cast the returned object into the proper
* class and object to convert must implement serializable
*/
import java.io.*;
public class ObjectSerializer
{
/**
* A generic method to convert objects into byte arrays.
* Object must implement Serializable
*
* @return Object an object that implements
Serializable
*
*/
public static byte[] serialize( Object obj )
throws IOException
{
ByteArrayOutputStream bao = new ByteArrayOutputStream
();
ObjectOutputStream oo = new ObjectOutputStream(
bao );

oo.writeObject( obj );
oo.flush();

byte[] serialized = bao.toByteArray();

oo.close();
bao.close();


return serialized;
}


/**
* A generic method to convert byte[] into an object (if
* the byte[] was an object). Object must implement
Serializable
*
* @return Object an object that implements
Serializable
*
*/
public static Object deserialize( byte[] bytes )
throws IOException, ClassNotFoundException
{
ByteArrayInputStream bao = new ByteArrayInputStream(
bytes );
ObjectInputStream oo = new ObjectInputStream( bao );

Object deserialized = oo.readObject();

oo.close();
bao.close();

return deserialized;
}

}

Enjoy,

Ron


>
> -----Original Message-----
> From: mozheyko_d [mailto:mozheyko_d@y...]
> Sent: Friday, December 17, 2004 7:08 AM
> To: Firebird-Java@yahoogroups.com
> Subject: [Firebird-Java] How2 save javaobject in Firebird ?
>
>
>
>
> Hi all !
>
> I need to save java-object in Firebird database
> I use:
> FirebirdSS-1.5.1 - Linux server
> FirebirdSQL-1.5.5JDK1.4 - JDBC driver
>
> Table:
>
> create table a(
> id integer not null primary key,
> rawdata blob
> );
>
> insert code:
>
> [code]
> ...
> PreparedStatement statement = connection.prepareStatement
("insert
> into a values(?, ?);");
> Object o = new SomeObject();
> statement.setObject(1, 1);
> statement.setObject(2, o);
> statement.executeUpdate();
> ...
> [/code]
>
> throws exception:
> [code]
> org.firebirdsql.jdbc.field.TypeConvertionException: Error
converting
> to object.
> at org.firebirdsql.jdbc.field.FBField.setObject
(FBField.java:741)
> at
> org.firebirdsql.jdbc.AbstractPreparedStatement.setObject
(AbstractPrepare
> dStatement.java:218)
> at Test.main(Test.java:37)
> [/code]
>
>
>
>
>
>
>
>
>
>
> Yahoo! Groups Links