3

I have a table like this

  list     |  id  | 
-----------+--------
 {930,23}  |  1   |
 {2012,1}  |  2   |
 {5943}    |  3   |
 {6148}    |  4   |
 {1003}    |  5   |

Now, I'd like to use JDBC to retrieve the first column. I've learned that we can use java.sql.Array to do that. (http://docs.oracle.com/javase/tutorial/jdbc/basics/array.html#retrieving_array), but when I declare the Array object like this:

import java.sql.Array;

while(rs.next())
    Array arr;

It issues a compile error:

[javac] /home/xxx.java:291: error: not a statement
[javac]             Array arr;
[javac]             ^
[javac] /home/xxx.java:291: error: ';' expected
[javac]             Array arr;
[javac]                  ^
[javac] /home/xxx.java:291: error: not a statement
[javac]             Array arr;
[javac]                   ^

It seems that Java is not recognizing the data type java.sql.Array. Anyone knows why? Thanks a lot!

1 Answer 1

2

You should use curly brackets with your while statements:

while (rs.next()) {
    Array arr;  // Add more code here.
}
Sign up to request clarification or add additional context in comments.

2 Comments

It works! Thank you. But why should we use curly brackets when there is only one statement?
@Yang: Because the Java specification doesn't all you to create a local variable there unless you have a block. Even if it were allowed, it wouldn't make much sense. What would its scope be?

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.