Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:57:03+00:00 2026-06-09T15:57:03+00:00

I want to update 5 fields of user personal information in a table called

  • 0

I want to update 5 fields of user personal information in a table called Personnel-table .

The user might want to update all the fields , part of them , or nothing at all .

The fields that the user might want to update are : FirstName , LastName ,Password ,UserName and Address .

That code does that :

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;
} 

But as you can see , the code is too big , since I run the query 5 times .

How can I tell mysql that if each of the fields is not the empty string "" , then update
the needed field , in one mysql-query ? is that possible or I must do that separately every time ?

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:57:04+00:00Added an answer on June 9, 2026 at 3:57 pm

    You can easily do this in a single query as:

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

    If your fields can have blank values like "" then you can use IF(? IS NOT NULL, ?, field_name) instead of blank.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two TIMESTAMP fields in my user table that I want to update
I'm building a table from which I want to update different fields of a
I want to update 2 of my database's fields according to user input.My code
I have a table named meeting_times with fields: meeting_date_time user_id I want to update
I want to accomplish this (mysql) query: UPDATE table SET field = field +
I want to uapdate a table field with an array.That means i have a
What I want: Update all new commits from server with my local repository in
I want to update an erb.html page in rails with information from a webscraping
I have a situation where we want to prevent the update of a table
I want to update a database in my asp.net application. The updated fields are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.