java向MySQL添加数据时中文乱码怎么解决?

2024-11-25 01:49:27
推荐回答(1个)
回答1:

遇到汉字先做转码,然后再插入选择,你数据库的编码类型的,如下例子有用

System.out.println("中文");//1
System.out.println("中文".getBytes());//2
System.out.println("中文".getBytes("GB2312"));//3
System.out.println("中文".getBytes("ISO8859_1"));//4
System.out.println(new String("中文".getBytes()));//5
System.out.println(new String("中文".getBytes(),"GB2312"));//6
System.out.println(new String("中文".getBytes(),"ISO8859_1"));//7
System.out.println(new String("中文".getBytes("GB2312")));//8
System.out.println(new String("中文".getBytes("GB2312"),"GB2312"));//9
System.out.println(new String("中文".getBytes("GB2312"),"ISO8859_1"));//10
System.out.println(new String("中文".getBytes("ISO8859_1")));//11
System.out.println(new String("中文".getBytes("ISO8859_1"),"GB2312"));//12
System.out.println(new String("中文".getBytes("ISO8859_1"),"ISO8859_1"));//13