Dmitry88
Dmitry88

Reputation: 47

SQL Exception . update row in jdbc

I need to write a query to update a row in the database. but exception is

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Erreur de syntaxe pr?s de 'SET eMail = '11111', SET phoneNumber = '111111' WHERE name = 'Saba', surname= 'M' ? la ligne 1 .

what is the problem?

public static void updateUser(User user, Connection connection) throws SQLException {
PreparedStatement ps = null;
ps = connection.prepareStatement("UPDATE USERS SET login = ?, SET eMail = ?, SET phoneNumber = ? WHERE name = ?, surname= ?");
        ps.setString(1, user.getLogin());
        ps.setString(2, user.geteMail());
        ps.setString(3, user.getPhoneNumber());
        ps.setString(4, user.getName());
        ps.setString(5, user.getSurname());
        ps.executeUpdate();

Upvotes: 0

Views: 240

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221106

The UPDATE statement only has a single SET clause. You repeated the SET keyword, which is wrong. Besides, you forgot the AND keyword to combine predicates. Write this instead:

try (PreparedStatement ps = connection.prepareStatement(
      "UPDATE USERS "
    + "SET login = ?, eMail = ?, phoneNumber = ? "
    + "WHERE name = ? AND surname = ?")) {

    // ...
}

Upvotes: 1

Related Questions