0

I have the follow:

  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)
  }

My intention is to use this method output to put into another PreparedStatement using .setArray(1, <array>)

But I'm getting the follow error: java.sql.SQLFeatureNotSupportedException

I'm using MySQL. Already tried INTEGER, INT, BIGINT. No success with none of then.

wviana
  • 1,619
  • 2
  • 19
  • 47

1 Answers1

0

Researching more found this:

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

So my solution was to create a temp table with just ids:

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

Than do inner join with other statments/queries to achieve same result:

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()
wviana
  • 1,619
  • 2
  • 19
  • 47