zoukankan      html  css  js  c++  java
  • java+mysql中文乱码解决

    MySQL默认使用 ISO-8859-1 ( 即Latin1 ) 字符集,而JAVA内部使用Unicode编码,因此在JAVA中向MYSQL数据库插入数据时,或者读取数据时,都需要先转换一下编码方式:

    插入数据:

    如:

    ...

    String str="中文";

    String sql = "insert into Tb (xxx) values (?)"

    PreparedStatement pstmt = conn.prepareStatement(sql);

    pstmt.setString(1,str);

    pstmt.executeUpdate();

    这样插入到Mysql数据库后,用mysql.exe连接查看数据可以看到,插入数据变成了几个“?”呈,也即成了乱码。

    插入数据的解决方法是:

    String str="中文";

    str = new String(str.getBytes(),"ISO8859_1");         //加入此句,改变编码为iso-8859-1

    String sql = "insert into Tb (xxx) values (?)"

    PreparedStatement pstmt = conn.prepareStatement(sql);

    pstmt.setString(1,str);

    pstmt.executeUpdate();

    读取数据时的解决方法:

    String str = new String(record.getString("wname").getBytes("ISO8859_1"));

  • 相关阅读:
    EnumMap实现类
    java enum
    openssl生成RSA公钥和私钥对
    rsa 公钥 私钥
    eclispe 通过git向码云上传
    eclipse git 报 git: 401 Unauthorized
    HttpClient 超时时间
    HttpClient 4 和 HttpClient 3 超时
    Java RSA 生成公钥 私钥
    万能适配器
  • 原文地址:https://www.cnblogs.com/xjqlove1989/p/3755448.html
Copyright © 2011-2022 走看看