【发布时间】:2019-05-20 22:56:07
【问题描述】:
这是我面临的一个问题:- 我正在尝试运行具有多个值的更新语句(preparedStatement),但前提是它们不为空。
例子:
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 语句)?
谢谢,
【问题讨论】: