首页  >  问答  >  正文

将标题重写为:通过获取ResultSet的id并将其作为java.sql.Array返回

<p>我有以下内容:</p> <pre class="brush:scala;toolbar:false;"> 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) } </pre> <p>我的意图是使用这个方法的输出放入另一个PreparedStatement中,使用<code>.setArray(1, <array>)</code></p> <p>但是我得到了以下错误:<code>java.sql.SQLFeatureNotSupportedException</code></p> <p>我正在使用MySQL。已经尝试了INTEGER,INT,BIGINT。但都没有成功。</p>
P粉905144514P粉905144514388 天前456

全部回复(1)我来回复

  • P粉265724930

    P粉2657249302023-09-04 14:49:07

    翻译后的内容为:

    研究后发现:

    所以我的解决方案是创建一个只包含id的临时表:

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

    然后与其他语句/查询进行内连接,以达到相同的结果:

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

    回复
    0
  • 取消回复