Convert java.util.List into java.sql.Array

前端 未结 2 875
情书的邮戳
情书的邮戳 2021-01-03 20:56

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

相关标签:
2条回答
  • 2021-01-03 21:19

    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.

    0 讨论(0)
  • 2021-01-03 21:30

    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.

    0 讨论(0)
提交回复
热议问题