请教如何在java程序中搞定mysql5乱码?

blddp 2006-12-30 03:20:05
环境:winXP sp2 简体中文, mysql 5.0, jdk5.0, eclipse3.2

代码片断
....
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/test?user=root&password=james&useUnicode=true&characterEncoding=UTF-8";
Connection conn= DriverManager.getConnection(url);
....

....
Connection conn=null;
ResultSet rs=null;
conn=this.getConnection();
PreparedStatement ps=conn.prepareStatement("select * from t_lession");
rs=ps.executeQuery();
rs.next();
String lId=rs.getString(1);
String lName=rs.getString(2);
System.out.println(lId);
System.out.println(lName.toCharArray());
System.out.println(new String(lName.getBytes("iso-8859-1"),"utf-8"));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{

try {
rs.close();
conn.close();
} catch (SQLException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
...全文
198 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
huihui0103 2006-12-31
  • 打赏
  • 举报
回复
将数据库默认设为utf8,连接字符串utf8,页面utf8,就OK了
For_suzhen 2006-12-31
  • 打赏
  • 举报
回复
插入前你先转成utf-8
取出来在转成GBK
没看到你转啊?
blddp 2006-12-30
  • 打赏
  • 举报
回复
插入,查询都是乱码
网上的方法试过一部分由于是老版本的,对mysql5不起作用

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧