2
這是我面臨的問題: - 我想運行一個更新語句(preparedStatement)與多個值,但僅當它們不爲空。JDBC更新語句非空
實施例:
String x, y,z; //initialized to some value
final String EXAMPLE_SQL = "Update Table1 set col1 = ?, col2 = ? where col3 = ?";
sqlStmt = conn.prepareStatement(EXAMPLE_SQL);
sqlStmt.SetString(1, x);
sqlStmt.SetString(2, y);
sqlStmt.SetString(3, z);
sqlStmt.executeUpdate();
但有時x可以是在這種情況下,我不想更新具有空值COL1列空。
一種方式可以是: -
if(x == null)
final String EXAMPLE_SQL = "Update Table1 set col2 = ? where col3 = ?";
else
final String EXAMPLE_SQL = "Update Table1 set col1 = ?, col2 = ? where col3 = ?";
有沒有一種方法,我可以使用JDBC(只用一個SQL語句)更乾淨做到這一點?
謝謝,