mysql update query sha512 mysqlsyntaxerrorexception

0aydgbwb  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(366)

有人知道为什么使用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
o4tp2gmn

o4tp2gmn1#

你有一个额外的右括号 ) 在更新代码中
试着把它改成 String query = "update user set password = sha2(?, 512) where id = "+getId();

5t7ly7z5

5t7ly7z52#

看来mysql的密码有问题。它可能会注意到password是mysql关键字。解决方法是这样写: password 但更好的解决方案是直接在java中散列密码。最好先做一个咸杂烩。

相关问题