我的MYSQL数据库中有一表tablea 其中字段name 为中文数据
tablea 的编码为系统默认的latin1_swedish_ci
在PHP中读写都正常
但是到了JAVA中不能正常显示
使用getByes转换却发现有些汉字转换不过来~555555555555
Statement stmt;
ResultSet rst;
stmt=conn.createStatement();
rst=stmt.executeQuery("select name from tablea where id=346");
while(!rst.isLast()){
rst.next();
System.out.println("name:"+new String(rst.getString("name").getBytes("ISO-8859-1"),"UTF-8"));
}
这是我写的代码
正常输出应该是:
name:电脑硬件
而实际输出为:
name:�??��??硬件该怎么转呢?
tablea 的编码为系统默认的latin1_swedish_ci
在PHP中读写都正常
但是到了JAVA中不能正常显示
使用getByes转换却发现有些汉字转换不过来~555555555555
Statement stmt;
ResultSet rst;
stmt=conn.createStatement();
rst=stmt.executeQuery("select name from tablea where id=346");
while(!rst.isLast()){
rst.next();
System.out.println("name:"+new String(rst.getString("name").getBytes("ISO-8859-1"),"UTF-8"));
}
这是我写的代码
正常输出应该是:
name:电脑硬件
而实际输出为:
name:�??��??硬件该怎么转呢?
解决方案 »
- java打包成exe可执行文件,点击后没反应
- Java是以什么方式进行翻译程序的?
- 菜鸟提问JAVA问题
- 最最初级的问题
- 关于jar文件使用?
- 想要孙鑫java/VC++完整视频教程的找我
- 请教关于JTree的刷新问题,高分(100)
- 请问怎么模仿String 写一个 字符串类 直接用等号接收一个字符串
- JRE 和 SDK 有什么区别?
- 求救:我在 JAVA 中用 TextField 输入文字时发现,在汉字方式下敲不了标点符号,而我用 VC 的 EDIT 却可以,为什么?
- 在线等:用那个函数获取客户端发来的UDP报文的IP地址和端口号?
- getitem和getitemselectable之间有什么区别呢?
name:�??��??硬件
~^~ 被CSDN转义了
实际结果就是电脑两个字变成乱码了
System.out.println("name:"+new String(rst.getString("name").getBytes("ISO-8859-1"),"gbk"));
试试行不~~
换成GBK也是乱码
你连SQL的代码贴出来看下
表和字段的编码都是latin1_swedish_ci
连接代码:
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/mysearch?user=root&password=123456");
我发现用outputstreamwriter输出正确但是我不知道怎么从outputstream写到String里面?
OutputStreamWriter out =new OutputStreamWriter(System.out,"utf-8");
byte[] bytes;
while(!rst.isLast()){
rst.next();
bytes=rst.getBytes("name");
out.write(new String(bytes));
//System.out.println("name:"+new String(rst.getString("name").getBytes("ISO-8859-1"),"UTF-8"));
}
out.flush();
out.close();这样写控制台输出居然是对的