将 ResultSet 和 return 的 ID 检索为 java.sql.Array

Retrive ids of ResultSet and return as java.sql.Array

我有以下内容:

  def getIds(name: String): java.sql.Array = {
    val ids: Array[Integer] = Array()
    val ps: PreparedStatement = connection.prepareStatement("SELECT id FROM table WHERE name = ?")
    ps.setString(1, name)
    val resultSet = ps.executeQuery()
    while(resultSet.next()) {
      val currentId = resultSet.getInt(1)
      ids :+ currentId
    }
    return connection.createArrayOf("INTEGER", ids.toArray)
  }

我的意图是使用此方法输出放入另一个 PreparedStatement using .setArray(1, <array>)

但我收到以下错误:java.sql.SQLFeatureNotSupportedException

我正在使用 MySQL。已经尝试过 INTEGER、INT、BIGINT。当时 none 没有成功。

进一步研究发现:

It seems that MySQL doesn't have array variables. May U can try temporary tables instead of array variables

所以我的解决方案是创建一个临时 table 只有 ids:

val idsStatement = connection.prepareStatement(
   "CREATE TEMPORARY TABLE to_delete_ids SELECT id FROM table WHERE name = ?")
idsStatement.setString(1, name)
idsStatement.executeUpdate()

与其他 statments/queries 进行内部连接以获得相同的结果:

val statementDeleteUsingIds = connection.prepareStatement(
    "DELETE to_delete_rows FROM table2 to_delete_rows INNER JOIN to_delete_ids tdi ON tdi.id = to_delete_rows.other_tables_id")
statementDeleteUsingIds.executeUpdate()