You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by Michael Mogley <mm...@adelphia.net> on 2003/11/23 04:56:56 UTC

trouble inserting blob into mysql

I have a MySQL table with a BLOB column.  

The corresponding field in the Java class is a byte array.  

The repository field-descriptor mapping is:

    <field-descriptor name="data" column="DATA" jdbc-type="BLOB"/>

When I try to do:

    persistenceBroker.store(objectWithBlob) 

I get the following stacktrace:


    Caused by: java.sql.SQLException: Unknown Types value
            at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:925)
            at com.mysql.jdbc.PreparedStatement.setObject(PreparedStatement.java:953)
            at org.apache.commons.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:254)
            at org.apache.ojb.broker.platforms.PlatformDefaultImpl.setObjectForStatement(PlatformDefaultImpl.java:265)
            at org.apache.ojb.broker.platforms.PlatformMySQLImpl.setObjectForStatement(PlatformMySQLImpl.java:80)
            at org.apache.ojb.broker.accesslayer.StatementManager.bindInsert(StatementManager.java:518)
            at org.apache.ojb.broker.accesslayer.JdbcAccessImpl.executeInsert(JdbcAccessImpl.java:231)

My version info:

- latest OJB from CVS as of today
- MySql 4.0.11a
- Connect/J 3.0.9 JDBC driver (production)

Lookiing at the driver source, it seems that the com.my.sql.jdbc.PreparedStatement.setObject method is one big switch statement with no branch for BLOB or CLOB types.  There IS however a PreparedStatement.setBlob method.

Am I doing something wrong in trying to use OJB to read/write BLOBS in MySQL?  Or is this a bug?

Any help is very appreciated.

Michael

P.S.  Glad to hear Matthew successfully banished the Oracle LOB headache!

Re: fixed PlatformMySQLImpl to support LOBs

Posted by Michael Mogley <mm...@adelphia.net>.
Cool!  Thanks for putting it in so quickly.

----- Original Message ----- 
From: "Jakob Braeuchi" <jb...@gmx.ch>
To: "OJB Developers List" <oj...@db.apache.org>
Sent: Sunday, November 23, 2003 2:23 AM
Subject: Re: fixed PlatformMySQLImpl to support LOBs


> hi michael,
>
> thanks for the patch. it's available in cvs now.
>
> jakob
>
> Michael Mogley wrote:
>
> > All,
> >
> > I believe I've fixed the problem.  The MySQL implementation was
completely
> > ignoring the special case of BLOB/CLOB.  I simply copied the LOB
treatment
> > in PlatformOracleImpl and merged this into PlatformMySQLImpl, with some
> > minor changes.  Have not fully tested but it works fine for what I'm
doing,
> > which is inserting images into blob fields.
> >
> > I'm not sure yet how to create patches, so here is the changed method in
> > PlatformMySQLImpl:
> >
> >     /*
> >      * @see Platform#setObjectForStatement(PreparedStatement, int,
Object,
> > int)
> >      */
> >     public void setObjectForStatement(PreparedStatement ps, int index,
> > Object value, int sqlType)
> >         throws SQLException
> >     {
> >         switch (sqlType)
> >         {
> >             case Types.BIT:
> >                  ps.setObject(index, value);
> >             break;
> >
> >             case Types.BLOB:
> >             case Types.LONGVARBINARY:
> >             case Types.VARBINARY:
> >                 if (value instanceof byte[]) {
> >                     byte buf[] = (byte[]) value;
> >                     ByteArrayInputStream inputStream = new
> > ByteArrayInputStream(buf);
> >                     ps.setBinaryStream(index, inputStream, buf.length);
> >
> >                     break;
> >                 }
> >
> >             case Types.CLOB:
> >                 Reader reader = null;
> >                 int length = 0;
> >
> >                 if (value instanceof String)
> >                 {
> >                     reader = new StringReader((String) value);
> >                     length = (((String) value)).length();
> >                 }
> >                 else if (value instanceof byte[])
> >                 {
> >                     byte buf[] = (byte[]) value;
> >                     ByteArrayInputStream inputStream = new
> > ByteArrayInputStream(buf);
> >                     reader = new InputStreamReader(inputStream);
> >                 }
> >
> >                 ps.setCharacterStream(index, reader, length);
> >             break;
> >
> >             default:
> >                  super.setObjectForStatement(ps,index,value,sqlType);
> >             break;
> >         }
> >     }
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
> > For additional commands, e-mail: ojb-dev-help@db.apache.org
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-dev-help@db.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Re: fixed PlatformMySQLImpl to support LOBs

Posted by Jakob Braeuchi <jb...@gmx.ch>.
hi michael,

thanks for the patch. it's available in cvs now.

jakob

Michael Mogley wrote:

> All,
> 
> I believe I've fixed the problem.  The MySQL implementation was completely
> ignoring the special case of BLOB/CLOB.  I simply copied the LOB treatment
> in PlatformOracleImpl and merged this into PlatformMySQLImpl, with some
> minor changes.  Have not fully tested but it works fine for what I'm doing,
> which is inserting images into blob fields.
> 
> I'm not sure yet how to create patches, so here is the changed method in
> PlatformMySQLImpl:
> 
>     /*
>      * @see Platform#setObjectForStatement(PreparedStatement, int, Object,
> int)
>      */
>     public void setObjectForStatement(PreparedStatement ps, int index,
> Object value, int sqlType)
>         throws SQLException
>     {
>         switch (sqlType)
>         {
>             case Types.BIT:
>                  ps.setObject(index, value);
>             break;
> 
>             case Types.BLOB:
>             case Types.LONGVARBINARY:
>             case Types.VARBINARY:
>                 if (value instanceof byte[]) {
>                     byte buf[] = (byte[]) value;
>                     ByteArrayInputStream inputStream = new
> ByteArrayInputStream(buf);
>                     ps.setBinaryStream(index, inputStream, buf.length);
> 
>                     break;
>                 }
> 
>             case Types.CLOB:
>                 Reader reader = null;
>                 int length = 0;
> 
>                 if (value instanceof String)
>                 {
>                     reader = new StringReader((String) value);
>                     length = (((String) value)).length();
>                 }
>                 else if (value instanceof byte[])
>                 {
>                     byte buf[] = (byte[]) value;
>                     ByteArrayInputStream inputStream = new
> ByteArrayInputStream(buf);
>                     reader = new InputStreamReader(inputStream);
>                 }
> 
>                 ps.setCharacterStream(index, reader, length);
>             break;
> 
>             default:
>                  super.setObjectForStatement(ps,index,value,sqlType);
>             break;
>         }
>     }
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-dev-help@db.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


fixed PlatformMySQLImpl to support LOBs

Posted by Michael Mogley <mm...@adelphia.net>.
All,

I believe I've fixed the problem.  The MySQL implementation was completely
ignoring the special case of BLOB/CLOB.  I simply copied the LOB treatment
in PlatformOracleImpl and merged this into PlatformMySQLImpl, with some
minor changes.  Have not fully tested but it works fine for what I'm doing,
which is inserting images into blob fields.

I'm not sure yet how to create patches, so here is the changed method in
PlatformMySQLImpl:

    /*
     * @see Platform#setObjectForStatement(PreparedStatement, int, Object,
int)
     */
    public void setObjectForStatement(PreparedStatement ps, int index,
Object value, int sqlType)
        throws SQLException
    {
        switch (sqlType)
        {
            case Types.BIT:
                 ps.setObject(index, value);
            break;

            case Types.BLOB:
            case Types.LONGVARBINARY:
            case Types.VARBINARY:
                if (value instanceof byte[]) {
                    byte buf[] = (byte[]) value;
                    ByteArrayInputStream inputStream = new
ByteArrayInputStream(buf);
                    ps.setBinaryStream(index, inputStream, buf.length);

                    break;
                }

            case Types.CLOB:
                Reader reader = null;
                int length = 0;

                if (value instanceof String)
                {
                    reader = new StringReader((String) value);
                    length = (((String) value)).length();
                }
                else if (value instanceof byte[])
                {
                    byte buf[] = (byte[]) value;
                    ByteArrayInputStream inputStream = new
ByteArrayInputStream(buf);
                    reader = new InputStreamReader(inputStream);
                }

                ps.setCharacterStream(index, reader, length);
            break;

            default:
                 super.setObjectForStatement(ps,index,value,sqlType);
            break;
        }
    }


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org