skip to Main Content

I am making a simple client banking application and I’m stuck here. I trying to update a row in the database I’m using eclipse, Dbeaver and PostqreSQL.

public Client updateClient (int clientId, AddUpdateDTO client) throws SQLException 
        
        try (Connection con = JDBCUtility.getConnection()){
            String sql = "UPDATE clients"
                    + "SET client_first_name = ?, "
                    + "client_last_name = ?, "
                    + "client_phone_number = ?, "
                    + "client_email"
                    + "WHERE"
                    + "client_id = ?; ";            
            PreparedStatement pst = con.prepareStatement(sql);
            
            pst.setString(1, client.getFirstName());
            pst.setString(2, client.getLastName());
            pst.setString(3, client.getPhoneNumber());
            pst.setString(4, client.getEmail());
            pst.setInt(5, clientId);
            
            int updatedAmount = pst.executeUpdate();
            
            if (updatedAmount != 1) {
                throw new SQLException("Unable to find client record with Id of " +                          clientId);
            }
        }
        
        return new Client(clientId, client.getFirstName(), client.getLastName(), client.getPhoneNumber(), client.getEmail());       
    }

Here is my database table:

CREATE TABLE clients (client_id SERIAL PRIMARY KEY,
    client_first_name VARCHAR(255) NOT NULL,
    client_last_name VARCHAR(255) NOT NULL,
    client_phone_number VARCHAR(255) NOT NULL,
    client_email VARCHAR(255) NOT NULL  
);

All my other queries work such as add, delete, getAll, and etc I just can’t figure this one out.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks guys that little mistake was killing me it works good now.


  2. I think you missed to assignment of the client_email field. Your query should look like that:

    String sql = "UPDATE clients"
                        + "SET client_first_name = ?, "
                        + "client_last_name = ?, "
                        + "client_phone_number = ?, "
                        + "client_email = ?"
                        + "WHERE"
                        + "client_id = ?; ";
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search