0

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]
3
  • It might help if you edit your question to add the code you're using so we can see how you're ending up with a java.lang.object. Commented Jun 26, 2015 at 15:39
  • Object is a superclass of every single class in Java, so that doesn't tell us much. And there are many ways to read SQL tables in Java. We'll need some more details (i.e. the code you're using) before we can answer this. Commented Jun 26, 2015 at 15:45
  • @Nanhydrin Please take a look. Thanks for your time. Commented Jun 26, 2015 at 15:49

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.