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

java 专栏收录该内容
13 篇文章 0 订阅

报错: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

要插入的表:





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 的时候,就能正常修改了。

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页

打赏作者

我就是那个大头

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值