解决mysql 修改报错: Incorrect string value: '\xAC\xED\x00\x05sr...' for column 'enabled' at row 1

报错:java.sql.SQLException: Incorrect string value: '\xAC\xED\x00\x05sr...' for column 'enabled' at row 1


我调用 下面update方法,SQL报错,提示java.sql.SQLException: Incorrect string value: '\xAC\xED\x00\x05sr...' for column 'enabled' at row 1

要插入的表:

解决mysql 修改报错: Incorrect string value: '\xAC\xED\x00\x05sr...' for column 'enabled' at row 1_第1张图片




public class UserDaoJDBC extends JdbcDaoSupport {

  private static final String SQL_INSERT = "insert into USER(username,password,authorities) values(?,?,?)";
  private static final String SQL_GET_BY_ACCOUNT = "select * from USER where username = ?";
  private static final String SQL_UPDATE = "update `USER` set "
      + "`password` = ?, authorities = ?, enabled = ?, "
      + "locked = ?, expired = ?, credentials_expired = ? where username = ?";
  private static final String SQL_DELETE = "delete from USER where username = ?";

  public void insert(User user) {
    UserDO udo = new UserDO(user);
    Object[] ps = new String[3];
    ps[0] = udo.getUsername();
    ps[1] = udo.getPassword();
    ps[2] = udo.getAuthority();
    getJdbcTemplate().update(SQL_INSERT, ps);
  }

  public User select(String username) {
    return getJdbcTemplate().query(SQL_GET_BY_ACCOUNT, new String[]{username}, rs -> {
      if (rs.next()) {
        boolean enabled = rs.getString(4).equalsIgnoreCase("y");
        return new User(
            rs.getString(1), rs.getString(2), enabled, new String[]{rs.getString(3)});
      }
      return null;
    });
  }

  public boolean update(User user) {
    UserDO udo = new UserDO(user);
    int effected = getJdbcTemplate().update(
        SQL_UPDATE,
        udo.getPassword(),
        udo.getAuthority(),
        udo.getEnabled(),
        udo.getLocked(),
        udo.getExpired(),
        udo.getCredentialsExpired(),
        udo.getUsername());
    return effected == 1;
  }

  public boolean delete(String username) {
    int effected = getJdbcTemplate().update(SQL_DELETE, username);
    return effected == 1;
  }


  @Getter @Setter
  class UserDO{
    private String username;
    private String password;
    private String authority;
    private String enabled = "Y"; 
    private String locked = "N";
    private String expired = "N";
    private String credentialsExpired = "N";

    public UserDO(User user) {
      setUsername(user.getUsername());
      setPassword(user.getPassword());
      setAuthority(user.getAuthorities()[0]);
      setEnabled(user.isEnabled()?"Y":"N");
      setLocked(user.isLocked()?"Y":"N");
      setExpired(user.isExpired()?"Y":"N");
      setCredentialsExpired(user.isCredentialsExpired()?"Y":"N");
    }
  }

}
 
  

最开始的时候,上面标红部分的代码是

char,而不是String,所以会报错,当我

改成String 的时候,就能正常修改了。

你可能感兴趣的:(java)