MySQL 更新查询 SHA512 MySQLSyntaxErrorException

MySQL UPDATE query SHA512 MySQLSyntaxErrorException

有人知道为什么带有 sha512 的 INSERT 可以正常工作吗:

String query = " insert into user (username, password, surname, lastname, email, admin)"
            + " values (?, sha2(?, 512), ?, ?, ?, ?)";
    System.out.println("query: " + query);
    // create the java statement
    PreparedStatement ps = conn.prepareStatement(query);
    ps.setString(1, newUser.getUsername());
    ps.setString(2, getPsw());
    ps.setString(3, newUser.getSurename());
    ps.setString(4, newUser.getLastname());
    ps.setString(5, newUser.getEmail());
......
......

更新查询不是:

String query = "update user set password = sha2(?, 512) where id = "+getId()+")";
    System.out.println("query: " + query);
    // create the java statement
    PreparedStatement ps = conn.prepareStatement(query);
    ps.setString(1, psw_edit);

我得到以下 MySQLSyntaxErrorException:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 1

mySQL 密码 似乎有问题。可能它注意到 password 作为 mySQL 关键字。解决方法是这样写:password

但更好的解决方案是直接在 JAVA 中散列密码。最好先创建一个加盐哈希。

您的更新代码中有一个额外的右括号 )

尝试将其更改为

String query = "update user set password = sha2(?, 512) where id = "+getId();