Array_agg From PostgreSQL To Java Array

634 Views Asked by At

I make a request from Java:

  select array_agg(symptoms.*) from symptoms where sickId=1;

I get the result:

    {(1,"Ларингит острый",t,t,t,t,t,t,t,t,f,f,f,f,f,f,f,f)}

Then I try to put it all into an array of objects in Java:

 Object[] myField = (Object[]) resultSet.getArray("array_agg").getArray();

He puts the whole result in the first cell of the array. And I need it to split the result into cells. For example:

    myField[0] = 1, myField[1]="Ларингит острый", myField[2] = true  etc

How to fix?

I'm trying to get a string from PostgreSQL with a specific index and convert it to an array of Object in Java.

1

There are 1 best solutions below

0
Renan.Silvano On

Maybe you can change your query to return the columns values each one in a row using unnest (see doc 9.18. Array Functions and Operators). Check this example:

Given you have a table like:

id type line
ba4323f9-bf91-4048-98b3-338dd45c810a PENDENTE Facetada
4ec17454-9f21-497c-bdc2-53e0951abeb8 PENDENTE Cilíndrica
8f7521ab-a559-4e9e-b26f-265d4741e78e PENDENTE Cilíndrica
bf67f77e-3052-4ea5-bf73-e947c5541f46 PENDENTE Cilíndrica
95717728-fd66-4e0d-841c-34dea9246af7 PENDENTE Cilíndrica

When you select your columns as an array and apply unnest function to show each column in a row.

select unnest(array[r.id::text,r.type, r.line]) columns_values from alamar.luminarias as r where id = 'ba4323f9-bf91-4048-98b3-338dd45c810a';

Result:

columns_values
ba4323f9-bf91-4048-98b3-338dd45c810a
PENDENTE
Facetada

Then you can get this results like a array and interact with it

(Object[]) resultSet.getArray("columns_values").getArray();