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

- 30,436
- 41
- 178
- 315

- 281
- 1
- 3
- 3
2 Answers
Use connection.createArrayOf(...)
For example:
final String[] data = yourList.toArray(new String[yourList.size()]);
final java.sql.Array sqlArray = connection.createArrayOf(typeName, data);
statement.setArray(position, sqlArray);
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.

- 234
- 2
- 12

- 588,226
- 146
- 1,060
- 1,140
-
2which is the _current_ Java. If his is lower, he should've stated that. – Bozho May 21 '10 at 07:46
-
1yes 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
-
5It may be interesting to note that with an Oracle database, this method cannot be used. See the [documentation here](http://docs.oracle.com/cd/B28359_01/java.111/b31224/oraarr.htm#i1056647) – Lukas Eder Feb 18 '12 at 08:56
-
The proposed code causes an error java.sql.SQLFeatureNotSupportedException, the answer is responded [https://stackoverflow.com/questions/24528337/passing-array-parameter-in-prepare-statement-getting-java-sql-sqlfeaturenotsu] – Shila Mosammami Mar 04 '22 at 15:17
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.

- 31
- 1