【发布时间】:2017-12-07 05:31:00
【问题描述】:
我正在尝试将一个字符串数组传递给准备好的语句,但它返回了这个异常:
java.sql.SQLFeatureNotSupportedException: This operation is not supported.
at com.microsoft.sqlserver.jdbc.SQLServerConnection.createArrayOf(SQLServerConnection.java:2763)
at entity.dao.getRecords(Dao.java:168)
at entity.dao.main(Dao.java:227)
我的代码是:
public List<Record> getRecords() throws SQLException {
String sql = "select * from table where clause in (?)";
PreparedStatement ps = this.connection.prepareStatement(sql);
List<String> strings = new ArrayList<>();
strings.add("string1");
strings.add("string2");
strings.add("string3");
Array array = this.connection.createArrayOf("VARCHAR", strings.toArray());
ps.setArray(1, array);
ResultSet executeQuery = ps.executeQuery();
List<Record> records = new ArrayList<Record>();
Record record;
while (executeQuery.next()) {
// ...
}
return records;
}
异常所在的行是Array array = this.connection.createArrayOf("VARCHAR", strings.toArray());
这是我尝试创建数组的时候。
我已经搜索过如何传递数组,每个人都告诉这样做,但似乎不适用于 SQLServer。
【问题讨论】:
-
使用这个:
ps.setString(1,"'comma', 'separated', 'values'"); -
这个例子只是说教。在我工作的场景中,我有一百个字符串。
-
@GordThompson 这篇文章不要谈论 sql-server。
-
根据堆栈跟踪,似乎 SQL Server JDBC 驱动程序不支持
createArrayOf()方法。 -
驱动的源代码在 GitHub 上。如果你查看堆栈跟踪,这个函数确实没有实现,只是在你的问题中抛出异常。
标签: java sql-server jdbc