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

Поиск
Список
Период
Сортировка
Искать

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

От:
vasylenko@uksatse.org.ua
Дата:



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.


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


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

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

It is generated inside method

void org.postgresql.jdbc2.AbstractJdbc2ResultSet.updateRowBuffer() ;


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


For all standart java.sql.Types method makes calls


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;
                }


but for the rest makes default action.

How can I modify my ArrayClass to avoid such exception?


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

От:
Kris Jurka <books@ejurka.com>
Дата:


On Mon, 16 Apr 2007, vasylenko@uksatse.org.ua wrote:

> I need to update (and insert too) an Array type into table via JDBC. Array
> consist of Strings;
>
> The method ResultSet -> setObject(i,arrayClassObject) � - �works well.
>
> But after the method ResultSet -> �updateRow() generate the exception
> ClassCastException.
>
> How can I modify my ArrayClass to avoid such exception?
>

Actually it's a mistake in the driver.  The switch needs to accomodate 
Types.ARRAY.  I've applied a fix to cvs for the 8.0, 8.1, 8.2, and head 
branches and will go out in the next release (hopefully in about a week). 
A jar file built with the fix is available here:

http://www.ejurka.com/pgsql/jars/va/

Kris Jurka
FAQ