Subject | Re: Data Type Mappings |
---|---|
Author | kkras2000 |
Post date | 2002-06-07T15:15:17Z |
Thank you for such a prompt response Roman,
I think I got it.
To save an object that implements Serializable to a BLOB field I do
the following
ByteArrayOutputStream buff = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buff);
out.writeObject(serializedObject);
out.flush();
stmt = conn.prepareStatement(queryString);
//send a byte[] as an Object
stmt.setObject(colIndex, buff.toByteArray());
stmt.executeUpdate();
.........
When reading a byte[] from the BLOB field can I do the reverse i.e.
stmt = conn.prepareStatement(queryString);
rs = stmt.executeQuery();
if (rs.next()) {
serializedObject = (SerializedObject) rs.getObject(colIndex);
}
or do I have to do the following?
stmt = conn.prepareStatement(queryString);
rs = stmt.executeQuery();
if (rs.next()) {
ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(rs.getBytes(1)));
serializedObject = (SerializedObject) in.readObject();
}
Thanks again for your help
Best regards
Kris
I think I got it.
To save an object that implements Serializable to a BLOB field I do
the following
ByteArrayOutputStream buff = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(buff);
out.writeObject(serializedObject);
out.flush();
stmt = conn.prepareStatement(queryString);
//send a byte[] as an Object
stmt.setObject(colIndex, buff.toByteArray());
stmt.executeUpdate();
.........
When reading a byte[] from the BLOB field can I do the reverse i.e.
stmt = conn.prepareStatement(queryString);
rs = stmt.executeQuery();
if (rs.next()) {
serializedObject = (SerializedObject) rs.getObject(colIndex);
}
or do I have to do the following?
stmt = conn.prepareStatement(queryString);
rs = stmt.executeQuery();
if (rs.next()) {
ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(rs.getBytes(1)));
serializedObject = (SerializedObject) in.readObject();
}
Thanks again for your help
Best regards
Kris
--- In Firebird-Java@y..., "rrokytskyy" <rrokytskyy@y...> wrote:
> Hi,
>
> I will try to answer your questions. In general we perform type
> maping according to table defined in JDBC specifications. In
> particular this means:
>
> > java.sql.ResultSet (or interbase.interclient.ResultSet) :
> > getBoolean(String columnName)
>
> if field is of:
>
> 1) one of numeric types (INTEGER, NUMERIC, DOUBLE PRECISION, etc),
> value of 1 means true, any other value means false;
>
> 2) CHAR or VARCHAR, value of 'Y' or 'TRUE' (ignoring the case)
means
> true, value of 'N' or 'FALSE' means false, otherwise you get
> SQLException;
>
> 3) any other type throws SQLException.
>
> > getByte(String columnName)
>
> We try to convert value to byte, if we're successful - we return
it,
> otherwise you get SQLException; we support conversion in numeric
> types and string types;
>
> > getObject(int columnIndex)
>
> depends on the type of the field:
>
> INTEGER - java.lang.Integer
> NUMERIC(18,0) - java.lang.Long
> DOUBLE PRECISION - java.lang.Double
> CHAR or VARCHAR - java.lang.String
> BLOB with custom type - java.sql.Blob
> BLOB SUB_TYPE 0 or 1 - byte[]
> TIME - java.sql.Time
> DATE - java.sql.Date
> TIMESTAMP - java.sql.Timestamp
>
> > and
> >
> > java.sql.PreparedStatement (or
> > interbase.interclient.PreparedStatement)
> > setBoolean(int parameterIndex,boolean x)
>
> same as in rs.getBoolean(...) but reversed;
>
> > setByte(int parameterIndex,byte x)
>
> numeric fields - store byte value;
> string fields - convert to string and store as string;
> time fields - SQLException
>
> > setObject(int parameterIndex, Object x)
>
> depends on the type of x, in general procedure reversed to one in
> rs.getObject(...) method.
>
> > In the InterClient javadocs of PreparedStatement interface I read
> > that the boolean is converted to SQL BIT the byte is converted to
> > SQL TINYINT, however in the Interbase docs I don't see either of
> > these data types. What am I missing?
>
> We try to convert data from the type of the column to the type
you're
> accessing. If no conversion is possible, we throw SQLException. We
> try to be intelligent here.
>
> > If I pass a Serialized object (implementing Serialized interface)
to
> > setObject(int parameterIndex, Object x) function what should be
the
> > corresponding data type in the Firebird Sql table?
>
> In general, BLOB. But we do not serialize objects. Here's the code
> that performs setObject(...):
>
> if (value == null) {
> setNull(true);
> return;
> }
>
> if (value instanceof BigDecimal) {
> setBigDecimal((BigDecimal) value);
> } else
> if (value instanceof Blob) {
> if (value instanceof FBBlob)
> setBlob((FBBlob)value);
> else
> setBinaryStream(((Blob) value).getBinaryStream(),
> (int)((Blob)value).length());
> } else
> if (value instanceof Boolean) {
> setBoolean(((Boolean) value).booleanValue());
> } else
> if (value instanceof Byte) {
> setByte(((Byte) value).byteValue());
> } else
> if (value instanceof byte[]) {
> setBytes((byte[]) value);
> } else
> if (value instanceof Date) {
> setDate((Date) value);
> } else
> if (value instanceof Double) {
> setDouble(((Double) value).doubleValue());
> } else
> if (value instanceof Float) {
> setFloat(((Float) value).floatValue());
> } else
> if (value instanceof Integer) {
> setInteger(((Integer) value).intValue());
> } else
> if (value instanceof Long) {
> setLong(((Long) value).longValue());
> } else
> if (value instanceof Short) {
> setShort(((Short) value).shortValue());
> } else
> if (value instanceof String) {
> setString((String) value);
> } else
> if (value instanceof Time) {
> setTime((Time) value);
> } else
> if (value instanceof Timestamp) {
> setTimestamp((Timestamp) value);
> } else {
> throw (SQLException) createException(
> OBJECT_CONVERSION_ERROR).fillInStackTrace();
> }
>
> You have to pass byte[] with serialized data. That's what
persistence
> manager usually does. We might add some code to accept
> java.io.InputStream, but AFAIK, we are not planning to add generic
> serialization mechanism.
>
> Hope this helps.
>
> Best regards,
> Roman Rokytskyy