Toms
Toms

Reputation: 239

How to write an sql query to update multiple columns of a table using prepared statements?

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.

Upvotes: 0

Views: 7728

Answers (2)

z atef
z atef

Reputation: 7679

("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());

Upvotes: 1

Ravinder Reddy
Ravinder Reddy

Reputation: 24002

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

Upvotes: 2

Related Questions