将 java.util.List<String> 转换为 java.sql.Array
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2880304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert java.util.List<String> into java.sql.Array
提问by John Jesudason D
How would you convert a java.util.List<String>
instance into a java.sql.Array
?
您如何将java.util.List<String>
实例转换为java.sql.Array
?
回答by Bozho
Use connection.createArrayOf(...)
用 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:
其中 typeName 是:
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
数组元素映射到的类型的 SQL 名称。typeName 是特定于数据库的名称,可以是该数据库支持的内置类型、用户定义类型或标准 SQL 类型的名称。这是 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 getarrays, not create them. If you want to, you can instantiate the implementing class from your jdbc driver, but this is non-portable.
如评论中所述,这是 Java 1.6。对于旧版本,您不能以独立于驱动程序的方式创建它。您应该只获取数组,而不是创建它们。如果需要,您可以从 jdbc 驱动程序实例化实现类,但这是不可移植的。
回答by Kris Jurka
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.
createArrayOf 的 type 参数是元素类型,而不是数组类型,因此您可能需要诸如“varchar”或“text”之类的东西。VARIADIC 是函数参数修饰符,而不是类型说明符。