写连接数据库的URL时指明characterEncoding为GB2312
下面写的是8859_1即ISO-8859-1也是默认的..String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
下面写的是8859_1即ISO-8859-1也是默认的..String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
解决方案 »
- 请问下 防止服务器中的文件被非法下载的问题
- 大家推荐一本比较好的servlet和jsp的学习书籍
- IIS6+Tomcat5.5整合问题
- 继承
- 请问JSP中出现的"${}"符号是啥含义阿?
- 谁能送我点可用分,我可以免费解答Java, JSP, Servlet, JDBC, Struts的问题(作为一个四角用户)
- struts中两个action 之间可以传递对象吗?复杂对象
- envelope.getResponse()获取不到值
- 初学者 servlet 问题
- 很菜的连接数据库JavaBean的问题(高分)
- jboos启动出错,信息如下。。。请达人给予解答。谢谢了!!
- 我从数据库中选择的数据怎样显示在页面上,请高手指点,最好给点程序
是不是:
String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=utf-8"
然后将MYSQL的数据库设为default-character-set=utf8就好了呢?
后记录集还是乱码呀,后面位大虾请教如何将mysql设为default-character-set=utf8??小弟对mysql不是很熟呀!
default-character-set=gb2312
即可
你还可以试试如下方法:插入数据库时,先把要插入的汉字字符串经过如下tran方法转码:
public String tran(String code)
{
String tmp= new String(code.getBytes(),"ISO-8859-1");
return tmp;
}
把tmp再插入数据库
code=new String(a);
if (str == null) {
return null;
}
try {
return new String(str.getBytes("ISO8859_1"), "GBK");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
http://expert.csdn.net/Expert/topic/1529/1529097.xml?temp=.9971582我简单总结一下:
数据库存储最好用8859_1的格式,
所以存入数据库的时候进行一下编码转换,但我们通常的显示格式为GB2312或GBK,所以取出来的时候再转一次例如:存入数据库时用:
把数据转成8859_1的格式
name=new String(name.getBytes("gb2312"),"8859_1");
content=new String(content.getBytes("gb2312"),"8859_1");insert into (name,content)values(?,?).......从数据库取的时候用:
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/gfqqqqpe?user=gfqqqqpe_f&password=aaaaa&useUnicode=true;characterEncoding=8859_1");
从数据库取的时候指定编码方式为“8859_1”,
然后显示的时候进行转换:
name=new String(name.getBytes("8859_1"),"gb2312");
这种方法在MYSQL上通用