0

I have been trying some thing like this:

PreparedStatement ps = connection.prepareStatment("UPDATE table_nm(col1,col2,col3,col4,col5) SET(?,?,?,?,?)");

ps.setString(1, textField1.getText());
ps.setString(2, textField2.getText());
ps.setString(3, textField3.getText());
ps.setString(4, textField4.getText());
ps.setString(5, textField5.getText());
ps.executeUpdate();

May i know what am i doing wrong here. I tried different ways, but none are working. I just want to update all the columns.

4
  • 2
    Learn basic sql. Your syntax is outright wrong. UPDATE table SET field1=value1, field2=value2, field3=value3, etc Commented Mar 16, 2014 at 5:50
  • i tried this too but its'nt updating. PreparedStatement ps = connection.prepareStatment("UPDATE table_nm SET col2=?, col3=?, col4=?, col5=? WHERE col1=?"); Commented Mar 16, 2014 at 6:04
  • It is not updating means what? Any error? Show error stack. Commented Mar 16, 2014 at 6:11
  • Thanks, the problem was There was no row inserted, i thought updating will automatically add the row. Thanks for the help. Its working when i inserted a row and perform the update statement. Commented Mar 16, 2014 at 6:16

2 Answers 2

2

As commented by @MarcB, the UPDATE statement in your code is wrong:

Change:

UPDATE table_nm(col1,col2,col3,col4,col5) SET(?,?,?,?,?)

To:

UPDATE table_nm 
   SET col1=?, col2=?, col3=?, col4=?, col5=?

Rest of your code seem to be fine.

Refer to: MySQL: UPDATE Syntax

Sign up to request clarification or add additional context in comments.

Comments

1
("UPDATE COFFEES SET col1 = ? , col2 =? , col3 =?  and So on .....")

FyI, You can also add where caluse like this WHERE col1 LIKE ?");

 PreparedStatement ps = connection.prepareStatment("UPDATE table_nm SET col1 = ? , col2 =? , col3 =? ")

  ps.setString(1, textField1.getText());
  ps.setString(2, textField2.getText());
  ps.setString(3, textField2.getText());

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.