我试图将String数组传递给预准备语句,但是它返回了这个异常:
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。
答案 0 :(得分:0)
您可以使用,或者,createQuery statemen(实体管理器)
String sql= "select * from Table table where table.clause IN :clauses";
Query query = em.createQuery(sql, Record.class);
List<String> strings = new ArrayList<>();
strings.add("string1");
strings.add("string2");
strings.add("string3");
query.setParameter("clauses", strings );
List<Record> result= query .getResultList();