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语句)更干净做到这一点?
谢谢,