jingjihui 发表于 2018-10-7 12:26:03

java mysql的latin1转UTF-8

  网上大部分方法:先usename latin;然后再
   view plain copy

[*]  System.out.println(new String(s.getBytes("ISO-8859-1"), "UTF-8"));
  尝试过以后,完全不好使。
  下面方法亲测有效:sql语句正常写,不需要添加use name,得到的string用以下函数转换
   view plain copy

[*]  public String convertCharset(String s) {
[*]  if (s != null) {
[*]  try {
[*]  int length = s.length();
[*]  byte[] buffer = new byte;
[*]  // 0x81 to Unicode 0x0081, 0x8d to 0x008d, 0x8f to 0x008f, 0x90
[*]  // to 0x0090, and 0x9d to 0x009d.
[*]  for (int i = 0; i < length; ++i) {
[*]  char c = s.charAt(i);
[*]  if (c == 0x0081) {
[*]  buffer = (byte) 0x81;
[*]  } else if (c == 0x008d) {
[*]  buffer = (byte) 0x8d;
[*]  } else if (c == 0x008f) {
[*]  buffer = (byte) 0x8f;
[*]  } else if (c == 0x0090) {
[*]  buffer = (byte) 0x90;
[*]  } else if (c == 0x009d) {
[*]  buffer = (byte) 0x9d;
[*]  } else {
[*]  buffer = Character.toString(c).getBytes("CP1252");
[*]  }
[*]  }
[*]  String result = new String(buffer, "UTF-8");
[*]  return result;
[*]  } catch (UnsupportedEncodingException e) {
[*]  e.printStackTrace();
[*]  }
[*]  }
[*]  return null;
[*]  }

页: [1]
查看完整版本: java mysql的latin1转UTF-8