Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How would you convert a java.util.List<String> instance into a java.sql.Array?

share|improve this question

2 Answers 2

Use connection.createArrayOf(...)

For example:

String[] data = yourList.toArray(new String[yourList.size()];
connection.createArrayOf(typeName, data);

Where typeName is:

the SQL name of the type the elements of the array map to. The typeName is a database-specific name which may be the name of a built-in type, a user-defined type or a standard SQL type supported by this database. This is the value returned by Array.getBaseTypeName


As noted in the comments, this is Java 1.6. For older versions you can't create this in a driver-independent way. You are only supposed to get arrays, not create them. If you want to, you can instantiate the implementing class from your jdbc driver, but this is non-portable.

share|improve this answer
    
Since java 1.6... –  pgras May 21 '10 at 7:44
1  
which is the current Java. If his is lower, he should've stated that. –  Bozho May 21 '10 at 7:46
    
yes you're right regarding the version of java, I just wrote my comment because I tried to look up the method in my bookmarked javadoc with is for 1.5 (as it is the version I have to use)... –  pgras May 21 '10 at 10:34
3  
It may be interesting to note that with an Oracle database, this method cannot be used. See the documentation here –  Lukas Eder Feb 18 '12 at 8:56

The type argument to createArrayOf is the element type, not the array type, so you probably want something like "varchar" or "text". VARIADIC is a function argument modifier, not a type specifier.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.