2013-07-19 78 views
0

是否有方法將語句1和語句2合併爲1個查詢?謝謝。jdbc聯合2 sql更新語句

String statement1 = "UPDATE thing SET status = ? WHERE id = ?"; 
String statement2 = "UPDATE thing SET error_message = ? WHERE id = ?"; 

PreparedStatement preparedStatement = connection.prepareStatement(statement1); 
preparedStatement.setInt(1,status); 
preparedStatement.setInt(2, id); 
connection.prepareStatement(statement2); 
preparedStatement.setInt(1,error_message); 
preparedStatement.setInt(2, id); 

回答

5

看起來您正在試圖爲ID設置2列相同的表。您可以更改查詢,如

"UPDATE thing SET status = ? ,error_message = ? WHERE id = ?" 

此外,如果2個更新語句更新不同的表,您可以在同一個事務中執行這兩個。通過這種方式,您可以確保承諾將發生,如果這兩個聲明成功更新表。 Check the example here

0

做這樣的:

String statement1 = "UPDATE thing SET status = ? ,error_message = ? WHERE id = ?"; 

    PreparedStatement preparedStatement = connection.prepareStatement(statement1); 
    preparedStatement.setInt(1,status); 
    preparedStatement.setInt(2,error_message); 
    preparedStatement.setInt(3, id); 
    preparedStatement.executeUpdate();