3

I have the following code to update a record. The code compiles however it skips everything in the try statement and displays the error message in the catch statement. I am not sure what it is that I am missing as it doesn't display any sort of syntax error.

try {           
    PreparedStatement st = db.con.prepareStatement("UPDATE item SET Name = ?, Size = ?, Price = ?, WHERE ItemCode = ?");
    st.setString(1, textArea_Code.getText());
    st.setString(2, textArea_name.getText());
    st.setString(3, textArea_size.getText());
    st.setString(4, textArea_price.getText());
    st.executeUpdate();

    JOptionPane.showMessageDialog(frame, "Updated");    

} catch (SQLException e ) {
    JOptionPane.showMessageDialog(frame, "update not successful");      
}

5 Answers 5

12

You're swallowing the exception, which is generally a bad idea. At the very least call e.printStackTrace() so that you have an output of the exception.

As it happens, you have a syntax error in your SQL statement: UPDATE item SET Name = ?, Size = ?, Price = ?, WHERE ItemCode = ? - remove the comma from after Price = ?.


To address the confusion about why the UPDATE statement still doesn't work, despite fixing the syntax error, allow me to explain in more detail (far easier to do this in the answer, rather than in comments).

The ? character in your SQL String is a placeholder for a value that you'll set with one of the various set_() methods (in your case, only ever setString(). Each placeholder is numbered with an index starting from 1 - the first ? that appears in your string represents index 1, the second represents index 2, etc.

Your SQL string looks like this:

UPDATE item SET Name = ?, // 1
Size = ?, // 2
Price = ? // 3
WHERE ItemCode = ? // 4

You're setting values for your placeholders like this:

st.setString(1, textArea_Code.getText()); // ItemCode is fourth in the SQL, should be 4
st.setString(2, textArea_name.getText()); // Name is first in the SQL, should be 1
st.setString(3, textArea_size.getText()); // Size is second in the SQL, should be 2
st.setString(4, textArea_price.getText()); // Price is third in the SQL, should be 3
Sign up to request clarification or add additional context in comments.

9 Comments

its such a silly mistake I have made thanks for making me realize :) however now it does display the message box "updated" but the changes doesn't take affect in the database... Do you know why this could be?
@FatmaTurk You have four placeholders in your SQL string, numbered 1 to 4. 1 is the value for Name, 2 is the value for Size, 3 is the value for Price and 4 is the value for ItemCode. Now look at the values you're passing in your st.setString() calls.
You can get the count of actual rows updated like this int updated=st.executeUpdate(); Check that count and see if anything is actually updated. If it returns >0 then check which database you are using, whether the automatic commit mode is on or not etc.
executeUpdate returns an int. You need to check that value to determine if anything was updated. If you are seeing a zero, I would check the database.
@Patrick if the issue was with the database then I assume it wouldn't allow me to delete a record from the database as well right?
|
4

Have you tried changing that UPDATE statement to something like this:

"UPDATE item SET Name = ?, Size = ?, Price = ? WHERE ItemCode = ?"

I took out the final , (comma) after the Price = ? because it may conflict with the rest of the SQL.

Edit:

Also, you may want to rearrange the textArea_Code.getText() line to be the last value for the prepared statement. Otherwise, the ItemCode value may not be matching up with the order of the ? placeholders (as the moment, it looks like textArea_price.getText() is getting used as the value for ItemCode... which might be dangerous for what gets updated!)

Instead, what about something like this:

PreparedStatement st = db.con.prepareStatement("UPDATE item SET Name = ?, Size = ?, Price = ? WHERE ItemCode = ?");
st.setString(1, textArea_name.getText());
st.setString(2, textArea_size.getText());
st.setString(3, textArea_price.getText());
st.setString(4, textArea_Code.getText());
st.executeUpdate();


In this way, all of the ? placeholders correctly match up with the values being set:

Name: st.setString(1, textArea_name.getText());
Size: st.setString(2, textArea_size.getText());
Price: st.setString(3, textArea_price.getText());
ItemCode: st.setString(4, textArea_Code.getText());


This is because parameters need to be in the same order as the placeholders (see this documentation for more information.)

4 Comments

I have made the change to my SQL statement but it still doesn't update the database..
@FatmaTurk I updated this answer; you may want to try rearranging the order of your placeholder strings. Please see answer for more details.
it works now :) now I can see how the order can also make a difference..thank you so much..
@FatmaTurk No problem; and... welcome to Stack Overflow! As you ask questions, don't forget to let the community know if an answer helps you by either upvoting an answer, or accepting the answer! Thanks~
0

What is the output of executeUpdate? Does it return any number of rows updated? If not, then obviously your update clause needs to be, well, updated!

If the executeUpdate does return a positive number, indicating that it updated number of rows, may be your commit policy is not set to auto commit. In that case you may want to commit after executeUpdate.

con.commit();

1 Comment

its sorted now the issue was the order of the setString calls
0
  1. Could you try to add a e.printStackTrace() in your catch block ?
  2. In your query remove the , before WHERE UPDATE item SET Name = ?, Size = ?, Price = ? WHERE ItemCode = ?
  3. Maybe there is a problem with your DB ... is your item table in the default schema?... the printStackTrace() will help to find the answer

Comments

0

After getting DB connection set auto commit to true so that DB will be updated without any errors.

connection = DBConnection.getInstance().getConnection();
connection.setAutoCommit(true);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.