简体   繁体   中英

How do I get my sql update method to work?

I am doing a method where it should update the location of a tag (card) I have in my local database, this method (UpdateWallet) takes the user's input and overwrite the record in my database according to what the user has inserted. at the end of this method code, if it's found the entered 'tag' in the database and update its location successfully then it should return true and print a message says the update has been successful. Otherwise, return false But I got 2 issues :

1-the values are not getting updated although I am getting that previous message (It has been updated successfully).

2- whatever values I input, my method is happy with and claims that it 'updates' the record. even if the tag inserted does not exist in my database. wherein this case it should just return false.

my update method:

public boolean UpdateWallet(wallet upWallet) throws SQLException {
    Connection dbConnection = null;
    Statement statement = null;

    String update = " UPDATE wallets  SET Location  = '"+upWallet.getWalletLocation()+"'  WHERE Tag = '"
    + upWallet.getWalletTag()+"';";

    try {
        dbConnection = getDBConnection();
        statement = dbConnection.createStatement();
        // execute SQL update
        statement.executeUpdate(update);
        System.out.println("It has been updated successfully");

    } catch (SQLException e) {

        System.out.println(e.getMessage());
        return false;

    } finally {

        if (statement != null) {
            statement.close();
        }
        if (dbConnection != null) {
            dbConnection.close();
        }
    }
    return true;
}

controller code:

System.out.println("Please enter a tag to update its location");
String tag = input.next();

System.out.println("Please enter Location ");
String loc = input.next();

wallet w = new wallet(tag,loc); 
WalletsDAO.UpdateWallet(w);

wallet class:

public class wallet {
    private String Name;
    private String Location;
    private String Tag;

    public wallet(String Name, String Location, String Tag) {
        this.Name = Name;
        this.Location = Location;
        this.Tag = Tag;
    }

    public wallet(String Tag) {
        this.Tag = Tag;

    }

    public wallet(String Location, String Tag) {
        this.Name = Name;
        this.Location = Location;
        this.Tag = Tag;
    }

    public String getWalletName() {
        return Name;
    }

    public void setWalletName(String Name) {
        this.Name = Name;
    }

    public String getWalletLocation() {
        return Location;
    }

    public void setWalletLocation(String Location) {
        this.Location = Location;
    }

    public String getWalletTag() {
        return Tag;
    }

    public void setWalletTag(String Tag) {
        this.Tag = Tag;
    }

    @Override
    public String toString() {
        String p = (this.Name + ',' + this.Location + ',' + this.Tag);

        return p;
    }
}

Can anyone help me out please?

You can look at javadoc for executeUpdate: https://docs.oracle.com/javase/7/docs/api/java/sql/Statement.html#executeUpdate(java.lang.String)

You should use int result of executeUpdate() in order to know if none or some value has been updated, not exception (there is no exception if no value was updated)

The problem is with the following call:

wallet w = new wallet(tag,loc); 

It should be as

wallet w = new wallet(loc,tag); 

matching the constructor public wallet(String Location, String Tag) .

On another note, from your code, remove/comment the commented line (ie this.Name = Name ) in the following code:

public wallet(String Location, String Tag) {
    //this.Name = Name;
    this.Location = Location;
    this.Tag = Tag;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM