Gabi Barrientos
Gabi Barrientos

Reputation: 1886

Updating MySQL table with JDBC issue

I have a piece of software where data gets inserted into a table, unless the data exists, in which case the table should get updated.
Inserting the data into the table causes no problems at all. I use the following code for that:

PreparedStatement ps = this.con.prepareStatement("INSERT INTO tbl_banned(banned_player, banned_reason, banned_by, banned_from, banned_to) VALUES (?, ?, ?, ?, ?)");
ps.setString(1, player.getName());
ps.setString(2, reason);
ps.setString(3, by);
ps.setString(4, currentDateTime);
ps.setString(5, toDateTime);

ps.executeUpdate();

However, when I try to update the same table with the same information using the following code:

PreparedStatement ps = this.con.prepareStatement("UPDATE tbl_banned SET banned_reason=?, SET banned_by=?, SET banned_from=?, SET banned_to=? WHERE banned_player=?");
ps.setString(1, reason);
ps.setString(2, by);
ps.setString(3, currentDateTime);
ps.setString(4, toDateTime);
ps.setString(5, player.getName());

ps.executeUpdate();

I get the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET banned_by='User', SET banned_from='2013/06/12 04:10:45', SET banned_to='2' at line 1

I can not seem to figure out what the reason is for this error, and I have been struggling with it for the past hour. If anybody could shed some light on the subject, that would be greatly appreciated.

Upvotes: 0

Views: 66

Answers (1)

Daniel Robertus
Daniel Robertus

Reputation: 1102

no need to set multiple time in a row. it should be :

PreparedStatement ps = this.con.prepareStatement("UPDATE tbl_banned SET banned_reason=?,  banned_by=?, banned_from=?, banned_to=? WHERE banned_player=?");

here is your guidance to use UPDATE command in mysql

Upvotes: 1

Related Questions