Updating rows by their unique identifier can be easily accomplished by utilizing the

I am currently facing an issue while trying to update a row by its Id using Java, MySQL, and AngularJS. The problem is that I keep receiving a false response and no changes are reflected in the database.

Below is my Java function:

public boolean modifyClient(int idclient, String fullName, String email, int accountNumber, String password) {
            try 
            {
                System.out.println("Editing client with full name: " + idclient);
                Connection con = Connexion.getConnection();

             // Update statement for MySQL
                String query = "UPDATE client SET nomcomplet = ?, mail = ?, numerocompte = ?, mdp = ? WHERE idclient = ?";

                PreparedStatement ps = con.prepareStatement(query);
                ps.setInt(1, idclient);
                ps.setString (2, fullName);
                ps.setString (3, email);
                ps.setInt (4, accountNumber);
                ps.setString (5, password);

                ps.executeUpdate();
                if(ps.executeUpdate() > 0) {
                    return true;

                }
                else 
                {
                    return false;
                }
            }

            catch (Exception e) 
            {
                    System.out.println("Error with modifyClient() -->" + e.getMessage());
                    return false;
            }
        }

Next is my controller function:

    @POST
        @Path("modifyClient/{idclient}/{fullName}/{email}/{accountNumber}/{password}")
        @Produces(MediaType.APPLICATION_JSON)
        @Consumes(MediaType.APPLICATION_JSON)
        public boolean modifyClient(@PathParam("idclient") int idclient, @PathParam("fullName") String fullName, @PathParam("email") String email,@PathParam("accountNumber") int accountNumber, @PathParam("password") String password) {
                 ClientDao dao = new ClientDao();
             return dao.modifyClient(idclient,fullName,email,accountNumber,password);
        } 

Lastly, here is my AngularJS function:

$http.post('rest/client/modifyClient/'+idclient+'/'+$scope.fullName+'/'+$scope.mail+'/'+$scope.accountNumber+'/'+$scope.password').then(function(data){
                    alert('modified '+data.data);

                })

Your assistance on this matter would be greatly appreciated. Thank you.

Answer №1

It appears that the index you are using in your PreparedStatement is incorrect. Make sure to check the order of indexes used in the code snippet provided below:

String query = " UPDATE client SET nomcomplet = ?, mail = ?, numerocompte = ?, mdp = ? WHERE idclient = ?";

PreparedStatement ps = con.prepareStatement(query);

ps.setString (1, nomcomplet);
ps.setString (2, mail);
ps.setInt (3, numerocompte);
ps.setString (4, mdp);

ps.setInt(5, idclient);

The correct index value depends on the order defined in the query itself.

Please note: You may encounter an exception when trying to set idClient = "apassword" as it seems that idClient should be a numeric value. The database will likely throw an error indicating that you cannot compare a string with a number.

Answer №2

Make the following adjustments to your code:

 ps.setString (1, fullName);
 ps.setString (2, email);
 ps.setInt (3, accountNumber);
 ps.setString (4, password);
 ps.setInt(5, clientId);

 int recordsUpdated = ps.executeUpdate();

 if( recordsUpdated > 0) {
     return true;
 }
 else {
     return false;
 }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Revoking existing Json Web Tokens when updating user information

Once a user logs in with their username and password, they receive an access_token containing the payload that includes their uniqueTokenIdentifier. This unique identifier is stored for each user in the database. If a user changes their password due to ac ...

I am encountering issues with running my Eclipse program in Java after exporting it

I've been trying to develop a text-based game using Eclipse, but I keep encountering an error when exporting the executable jar file. The error message reads "could not find or load main class." As a beginner, I might be missing something obvious, so ...

The functionality of Nodejs exec is malfunctioning

I need assistance with executing DOS commands using the exec function: java -jar D:\selenium\selenium-server-standalone-2.40.0.jar -htmlSuite "*firefox3 C:\Users\AppData\Local\Mozilla Firefox\firefox.exe" "http://google. ...

The initial ajax request may sometimes return as 'undefined' during its first call

I have been working on creating a text input help feature similar to a desktop using the datatable.in table with keyboard navigation. In order to achieve this, I am dynamically changing the data source and also altering the header column. I have been succe ...

Tips on defining the specific CSS and JavaScript code to include in your Flask application

I am currently working on a web application using Flask and I need to specify which CSS and JS files should be included in the rendered HTML page based on a condition. There are times when I want to include mycss1.css and myjs1.js: <link href="/sta ...

Tips for effectively utilizing CSS classes with current-device

Attempting to lock the screen orientation in portrait mode for mobile and iPad devices using a CSS snippet found on css-tricks.com. @media screen and (min-width: 320px) and (max-width: 767px) and (orientation: landscape) { html { transform: rotate ...

The Date Input field fails to be populated when a date is chosen

I have been working on a function to automatically determine a person's age based on their selected date of birth. <div class="col-sm-3"> <label class="control-label">DOB</label> <date-input name=&q ...

Implement jQuery to toggle a class on click for added functionality

I am attempting to create a box that changes color when clicked. When the box is first clicked, it will turn red by adding the class red, and if clicked again, it will change to blue. The colors alternate with each click, but I am unsure of how to achieve ...

Performing a selection from an SQL database using the average function

I have a database structured like this: id, name, var1 and I'm looking to create a SQL query that looks like this: SELECT name FROM table WHERE last var1 > average of vars of each name I'm trying to select names where the last var1 is gr ...

jQuery floating sidebar that dynamically adjusts position based on content overflow

I've come across an interesting scenario with a fiddle that I'm working on: http://jsfiddle.net/yFjtt/1/ The main concept is to allow users to scroll past the header and have the sidebar 'stick' in place as they continue scrolling down ...

Personalizing the text color in IntelliJ IDEA's "run" code area

When you run the code you write in Intellij IDEA, is it possible to tailor the font color in the "run" section? ...

Utilize the functionName() method within a different function

There is a function this.randomNumber() that produces a random number like 54851247. The task at hand is to access this function within another function. console.log(this.randomNumber()); // Output: 54851247 function anotherFunction() { console.log(t ...

Unable to implement multiple draggable inner objects using Angular 5 and dragula library

After struggling for the past few days, I can't seem to get it to work... Here is a brief explanation of my issue: In this example, I have an array of objects structured like this: public containers: Array<object> = [ { "name": "contain ...

Verify the type of email domain (personal or corporate)

Here's an example: isPersonalEmail("<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c0aea1ada580a7ada1a9aceea3afad">[email protected]</a>") // true isPersonalEmail("<a href="/cdn-cgi/l/email- ...

What is the best approach for dynamically accessing or rendering Children within an Angular Component?

I am facing an issue with reusing a component called wrapper with different child components. I found some helpful resources such as this SO question and this article. However, these only address cases where the child component is known in advance. In my s ...

Encountering issues with installing GWT on Eclipse Juno

After numerous attempts to install GWT, I continue to encounter the same error message. Despite following advice from various sources such as running Eclipse as an administrator and updating Eclipse itself, the problem persists. My current setup includes ...

Striped shadows in React Three Fiber

Currently in the process of developing a basic 3D object viewer website using Next.js and React-Three-Fiber. Everything was running smoothly until I added a DirectionalLight instance and attempted to make all meshes receive shadows. https://i.sstatic.net/ ...

How come the index variable doesn't show the index in *ngFor loop in Angular 2?

When working with ng-repeat in Angular 1 to display the index, this code is used: <div ng-repeat="car in cars"> <ul> <li>Index: {{$index+1}}</li> <li>Car Name:{{car.name}}</li> </ul> </div> However, w ...

What is the best way to eliminate a trailing backslash from a string?

Currently, I am iterating through a list of Discord server names in node.js. The goal is to create a php file that contains an array structured like this: <?php $guildLookup = array( "164930842483761" => "guildName1", "56334 ...

The velocity of Google Cloud Storage resumable uploads

I have a query about the upload speed to Google Cloud Storage when using resumable uploads. A while ago, I developed a desktop Java client for my company to upload large files to GCS. This client had some specialized features which is why we didn't us ...