求救,用jdbc插入数据的时候,中文出现乱码如何解决?

robbie235 2004-10-31 11:44:49
我使用的数据库是MYSQL,我的代码如下:

import java.sql.*;
public class InsMySQL {
public static void main(String agrs[]) {
String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql://localhost:3306/jxgl";
String user = "sb";
String password = "songbo";
try {
Class.forName(driver);
}
catch(Exception e) {
System.out.println("无法加载驱动程序:" + driver);
}
try {
Connection conn = DriverManager.getConnection(url,user,password);
if(!conn.isClosed())
System.out.println("数据库连接成功!");
Statement smt = conn.createStatement();
smt.executeUpdate("INSERT into teacher(t_no,t_name,t_sex,t_birthday,tech_title)" +
"values('000011','张燕,'女','1967-06-06','讲师')");
System.out.println("记录插入完毕!");
smt.close();
conn.close();
}
catch(SQLException ee) {
System.out.println("数据库连接失败!");
}
}
}
...全文
249 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
eo 2004-11-01
  • 打赏
  • 举报
回复
进行一下编码的转换
String str2= new String(str1.getBytes(),"8859_1");
robbie235 2004-11-01
  • 打赏
  • 举报
回复
谢谢各位,通通得分
will52000 2004-11-01
  • 打赏
  • 举报
回复
楼上的可以
herman~~ 2004-11-01
  • 打赏
  • 举报
回复
试下这样吧; s = new String(s.getBytes("iso-8859-1"), "gb2312");
mengcool 2004-11-01
  • 打赏
  • 举报
回复
转码
同时看看你数据库字符集如何 一般要往数据库字符集方向转
IEQQ 2004-11-01
  • 打赏
  • 举报
回复
把你的字符串用这个函数处理一遍就OK了, 不行你找我.


public String getCNString(String strEnglish)
{
String strTemp = null;
byte[] baTemp = null;
try
{
//=====================================================
// 注意下面的 ISO-8859-1 可以用 GBK , 或者 GB2312替换
//======================================================
baTemp = strEnglish.getBytes("ISO-8859-1");
strTemp = new String(baTemp);
}
catch (UnsupportedEncodingException ex)
{
strTemp = null;
}
return strTemp;
}
xiaohaiz 2004-11-01
  • 打赏
  • 举报
回复
。。。错了,应该从数据库的字符集入手。。
比如设置mysql: default-character-set=gbk 之类
drugon 2004-11-01
  • 打赏
  • 举报
回复
插入的时候可以不处理,但是从数据库中读出来的时候是要转换的,不然的话就会出问题。
追求自由 2004-11-01
  • 打赏
  • 举报
回复
一、做编码转换,
二、在web.xml中用filter处理,就不需要你再每页都处理了。
dlxu 2004-11-01
  • 打赏
  • 举报
回复
在读出来的时候重新编码转换成gb2312

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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