將標題重寫為:透過取得ResultSet的id並將其作為java.sql.Array傳回
P粉905144514
P粉905144514 2023-09-03 18:10:50
0
1
461
<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粉905144514
P粉905144514

全部回覆(1)
P粉265724930

翻譯後的內容為:

研究後發現:

所以我的解決方案是建立一個只包含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()
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!