4

我想更新5名为Personnel-table.

用户可能想要更新所有字段、部分字段或根本不更新。

用户可能想要更新的字段 是:FirstName、、、和。LastNamePasswordUserNameAddress

该代码可以做到这一点:

public boolean updateUserInfo(String _idnumber , String _usernameNew , String _passwordNew ,  
        String _addressNew , String _firstNameNew , String _lastNameNew) throws SQLException
{

    ResultSet resultSet = null;

    String sqlStatement = "UPDATE PersonnelTable set `UserName` = ? WHERE `IdNumber` = ?";
    this.m_prepared = (PreparedStatement) this.m_connection.prepareStatement(sqlStatement);
    this.m_prepared.executeUpdate("USE Personnel");

    // set the ID number & account number 


    /**
     *  first 
     */
    m_prepared.setString(1, _usernameNew);
    m_prepared.setString(2, _idnumber);

    // execute first query - update password
    if (_usernameNew!= "")
    {
        resultSet = m_prepared.executeQuery();
        return true;
    }

    /**
     *  Second 
     */

    sqlStatement = "UPDATE PersonnelTable set `FirstName` = ? WHERE `IdNumber` = ?";
    this.m_prepared = (PreparedStatement) this.m_connection.prepareStatement(sqlStatement);
    m_prepared.setString(1, _firstNameNew);
    m_prepared.setString(2, _idnumber);

    if (_firstNameNew != "")
    {
        resultSet = m_prepared.executeQuery();
        return true;
    }


    /**
     *  Third
     */

    sqlStatement = "UPDATE PersonnelTable set `LastName` = ? WHERE `IdNumber` = ?";
    this.m_prepared = (PreparedStatement) this.m_connection.prepareStatement(sqlStatement);
    m_prepared.setString(1, _lastNameNew);
    m_prepared.setString(2, _idnumber);

    if (_lastNameNew!= "")
    {
        resultSet = m_prepared.executeQuery();
        return true;
    }

    /**
     *  Fourth
     */

    sqlStatement = "UPDATE PersonnelTable set `Address` = ? WHERE `IdNumber` = ?";
    this.m_prepared = (PreparedStatement) this.m_connection.prepareStatement(sqlStatement);
    m_prepared.setString(1, _addressNew);
    m_prepared.setString(2, _idnumber);

    if (_addressNew!= "")
    {
        resultSet = m_prepared.executeQuery();
        return true;
    }



    /**
     *  Fifth
     */

    sqlStatement = "UPDATE PersonnelTable set `Password` = ? WHERE `IdNumber` = ?";
    this.m_prepared = (PreparedStatement) this.m_connection.prepareStatement(sqlStatement);
    m_prepared.setString(1, _passwordNew);
    m_prepared.setString(2, _idnumber);

    if (_passwordNew!= "")
    {
        resultSet = m_prepared.executeQuery();
        return true;
    }



    return false;
} 

但正如您所见,代码太大了,因为我运行了 5 次查询。

我如何告诉 mysql 如果每个字段都不是空字符串"",那么在一个 mysql-query 中更新所需的字段?这是可能的还是我每次都必须分开做?

问候

4

1 回答 1

5

您可以在单个查询中轻松执行此操作,如下所示:

UPDATE PersonnelTable
SET FirstName = IF(? <> "", ?, FirstName),
    LastName = IF(? <> "", ?, LastName),
    Password = IF(? <> "", ?, Password),
    UserName = IF(? <> "", ?, UserName),
    Address = IF(? <> "", ?, Address)
WHERE IdNumber = ?;

如果您的字段可以有空白值,""那么您可以使用IF(? IS NOT NULL, ?, field_name)而不是空白。

于 2012-08-10T06:01:10.743 回答