Rafa Ayadi
Rafa Ayadi

Reputation: 347

How to access to data stored into java.lang.object?

I have a table product(id, name, cost) and a query

Select id, name from product where name='TV'

which returns two rows.
The result of this query is stored into a list of java.lang.object (list contains 2 objects containing the data I need).

How can I access to product.name and product.id?

List <Object> result = catalogServiceLocal.customQueryProd("s4"); //This is the query I am calling to execute SQL code.

    System.out.println("Size of List  "+result.size());
    System.out.println("List " + result.toString());

And the result is :

6:39:42,877 INFO  [stdout] (MSC service thread 1-7) Size of List    2
16:39:42,878 INFO  [stdout] (MSC service thread 1-7) List    [[Ljava.lang.Object;@661b6318, [Ljava.lang.Object;@1006f107]

Upvotes: 0

Views: 2279

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109577

I think the following should do:

for (Object record : result) {
    Object[] fields = (Object[]) record;
    String id = (String) fields[0];
    String name = (String) fields[1];
    System.out.printf("id: %s, name: %s%n", id, name);
}

The toString shows that result is a list of two Object arrays. Two records evidently. Every object array might be id+name fields, probably of type String.

Types in java use a short-hand: [ for array.

Upvotes: 1

Related Questions