Thread: Advice fetching Array data with JDBC 8.3 driver

Advice fetching Array data with JDBC 8.3 driver

From
"Woody Woodring"
Date:
Upgrading to the latest 8.3 JDBC driver broke one of our queries (that we
have found) that involves arrays in the database.  Reverting to the latest
8.2 driver fixes it.

The table is:

                                   Table "public.cpe_health"
    Column    |    Type    |                             Modifiers
--------------+------------+------------------------------------------------
-------------------
 cpe_healthid | integer    | not null default
nextval('cpe_health_cpe_healthid_seq'::regclass)
 mac          | macaddr    |
 polldate     | integer    |
 health       | smallint   |
 data         | integer[]  |
 alarm        | smallint[] |

My query is:

SELECT mac, CASE health WHEN 0 THEN 'red' WHEN 1 THEN 'yellow' WHEN 3 THEN
'green' ELSE 'blue' END AS health,
       array_upper(data,1) AS datasize, data, alarm
   FROM cpe_health;

The exception I get is:

java.lang.ClassCastException: [Ljava.lang.Integer;
        at
net.iglass.jglass.cpe.dao.CpeDataDAO$14.handle(CpeDataDAO.java:1018)
        at net.iglass.db.QueryRunner.query(QueryRunner.java:315)
        at net.iglass.db.QueryRunner.query(QueryRunner.java:336)
        at
net.iglass.jglass.cpe.dao.CpeDataDAO.getHealthData(CpeDataDAO.java:1042)

Which line 1018 in my code is:
 int[] hdata = (int[])(rs.getArray("data")).getArray();

Any advice on how to properly fetch the array data in my code would be
appreciated.

Thanks
Woody

----------------------------------------
iGLASS Networks
3300 Green Level Rd. West
Cary NC 27519
(919) 387-3550 x813
www.iglass.net


Re: Advice fetching Array data with JDBC 8.3 driver

From
Kris Jurka
Date:
Woody Woodring wrote:
> Upgrading to the latest 8.3 JDBC driver broke one of our queries (that we
> have found) that involves arrays in the database.  Reverting to the latest
> 8.2 driver fixes it.
>
>  int[] hdata = (int[])(rs.getArray("data")).getArray();
>

The 8.3 driver is returning this as Integer[] instead of the primitive
int[] because the server allows null values in arrays.  While null array
elements are possible in the server since 8.2, the driver didn't get
updated until 8.3.  You can change your code or provide the
"compatible=8.2" URL parameter to your connection URL.

Kris Jurka