2015-11-08 41 views
0

我需要編寫一個查詢來更新數據庫中的一行。但例外的是SQL異常。更新jdbc中的行

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:ERREUR德syntaxe PR小號去「設置電子郵件= '11111',SET phoneNumber的= '111111' WHERE name = '薩巴',姓='M'? la ligne 1。

是什麼問題?

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

回答

1

UPDATE聲明只有一個SET子句。你重複了SET關鍵字,這是錯誤的。此外,您忘記了AND關鍵字來組合謂詞。寫這個代替:

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

    // ... 
} 
+0

所以它的工作。謝謝。 – Dmitry88