How to modify my class inherited from java.sql.Array

From: vasylenko(at)uksatse(dot)org(dot)ua
To: pgsql-jdbc(at)postgresql(dot)org
Subject: How to modify my class inherited from java.sql.Array
Date: 2007-04-16 14:35:19
Message-ID: OFA61AD923.3DD16902-ONC22572BF.004EF6A1-C22572BF.00500731@UKCC.UKSATSE
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Hi!
I need to update (and insert too) an Array type into table via JDBC. Array
consist of Strings;

I created class with methods toString.

<CODE>
public class ArrayClass implements Array {
        private Object[] anArray;

        public int getBaseType() throws SQLException {
                // TODO Auto-generated method stub
                return
java.sql.Types.VARCHAR;//org.postgresql.core.types.PGUnknown;

        }

        public String getBaseTypeName() throws SQLException {

                // TODO Auto-generated method stub
                return "text";
        }

        public String toString()
        {
                String temp ="{";
                for(int i=0;i<anArray.length;i++)
                {
                        temp += anArray[i].toString();
                        if(i+1<anArray.length)
                                temp += ",";
                }
                temp+="}";
                return temp;
        }

}

<CODE/>

The method ResultSet -> setObject(i,arrayClassObject)   -  works well.

But after the method ResultSet ->  updateRow() generate the exception
ClassCastException.

It is generated inside method
<CODE>
void org.postgresql.jdbc2.AbstractJdbc2ResultSet.updateRowBuffer() ;
<CODE/>

because the java can't convert my ArraClass object to byte[].

For all standart java.sql.Types method makes calls

<CODE>
switch ( getSQLType(columnIndex + 1) )
                {

                case Types.DECIMAL:
                case Types.BIGINT:
                case Types.DOUBLE:
                case Types.BIT:
                case Types.VARCHAR:
                case Types.SMALLINT:
                case Types.FLOAT:
                case Types.INTEGER:
                case Types.CHAR:
                case Types.NUMERIC:
                case Types.REAL:
                case Types.TINYINT:
                case Types.OTHER:
                    rowBuffer[columnIndex] =
connection.encodeString(String.valueOf( valueObject));
                    break;

                //
                // toString() isn't enough for date and time types; we must
format it correctly
                // or we won't be able to re-parse it.
                //

                case Types.DATE:
                    rowBuffer[columnIndex] =

connection.encodeString(connection.getTimestampUtils().toString(null,
(Date)valueObject));
                    break;

                case Types.TIME:
                    rowBuffer[columnIndex] =

connection.encodeString(connection.getTimestampUtils().toString(null,
(Time)valueObject));
                    break;

                case Types.TIMESTAMP:
                    rowBuffer[columnIndex] =

connection.encodeString(connection.getTimestampUtils().toString(null,
(Timestamp)valueObject));
                    break;

                case Types.NULL:
                    // Should never happen?
                    break;

                default:
                    rowBuffer[columnIndex] = (byte[]) valueObject;
                }
<CODE/>

but for the rest makes default action.

How can I modify my ArrayClass to avoid such exception?

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Kris Jurka 2007-04-16 15:36:43 Re: Bug in timezone-parsing?
Previous Message James House 2007-04-16 14:24:48 Prepared Statements: Inefficient Type Conversion?