你的位置:
问答吧
-> JSP
-> 问题详情
Jsp链接mysql数据库,不能进行中文查询
各位同学大家好,我现在用的是ImHosted.com的jsp主机,不过网页上传完之后,不能进行中文的查询,类似select id from table where name='朋友',这样的查询,返回的记录为空。而我在本地电脑调试,一切正常,我在网上查的是,需要设置my.ini文件,但是我给他们客服发邮件,他们说:We would like to inform you that to can change the default character set from your cpanal --> phpmyadmin。可是在phpmyadmin中怎么设置啊?
我数据库用的是utf8_unicode_ci,jsp页面编码全都是utf8的,数据库连接也加上了useUnicode=true&characterEncoding=utf8,请问如何来解决?非常感谢各位。
作者: ghostgarlic
发布时间: 2010-08-26
哪里有同学!
不能进行中文查询,怀疑是乱码在作怪!你可以查查!
作者: qjsjp
发布时间: 2010-08-26
数据库中的记录,用phpmyadmin查看,一切正常,如果select name from table where id=2,查询出来显示的记录都是中文,没有乱码。就是查询的时候,如果查询语句中有中文字符,查询语句就不执行。我已经查了很久了,都没有搞定...好崩溃
作者: ghostgarlic
发布时间: 2010-08-26
作者: qianquan
发布时间: 2010-08-26
应该是编码的问题,你尝试下加个过滤器试试看!
作者: closewbq
发布时间: 2010-08-26
非常感谢closewbq,我刚才试了一下,如果试图将中文字符写入数据库的时候,数据库中的文字都是问号?????。。。估计还是编码的问题,我再试一下。
作者: ghostgarlic
发布时间: 2010-08-26
中文乱码导致。
既然本地是ok的,那么,只要设置一下服务器mysql的字符集即可。服务器的数据库、数据表都设置为和你本地一样的
作者: whut_lcy
发布时间: 2010-08-26
服务器的数据库的字符集都是utf8,数据表也是utf8,只有my.ini(my.cnf)没有办法设置,不知道,客服不给设置。
作者: ghostgarlic
发布时间: 2010-08-26
你在程序得到的中文字符为???? 那不是数据库问题了.
你在程序里转下看看.
URLEncoder.encode("页面中文", "UTF-8")
作者: kimsung
发布时间: 2010-08-26
很有可能是你JSP页面用的编码方式不对,建议改为GB2312试试看Java code
request.setCharacterEncoding("GB2312");
作者: thegodofwar
发布时间: 2010-08-26
在执行查询前,先执行“set names utf8”
作者: aiur
发布时间: 2010-08-26
又是一个乱码问题,杯具
作者: fyjava1984
发布时间: 2010-08-26
在编写的时候,应该做到开发环境、数据库、JSP页面的编码方式保持一致,
你用show variables like 'character\_set\_%';这个命令查看数据库编码方式
如果设置的是UTF-8,应该显示出了filesystem是binary外,其他的都应该是UTF-8,
如果不是,建议重新安装MySQL,安装时一定要选择UTF-8编码方式(有一个设置界面,选择第三个,自主选择编码方式)。
作者: Li_ZiYue
发布时间: 2010-08-26
show variables like 'character\_set\_%';后,显示:
Variable_name Value
character_set_client utf8
character_set_connection utf8
character_set_database utf8
character_set_results utf8
character_set_server latin1
character_set_system utf8
好像是服务器编码有问题,在phpmyadmin中怎么修改?
作者: ghostgarlic
发布时间: 2010-08-26
在执行查询前,先执行“set names utf8”
作者: super_stan
发布时间: 2010-08-26
解决乱码问题,有几种途径
1. 安装mysql时在选择编码的时候设置
2. 配置连接的时候在url中设置
3. 过滤器
......
作者: yzy2010yzy
发布时间: 2010-08-26
1.mysql这么简单。安装也就3分钟的时间。
重新安装下了。
记得安装的时候选择默认的字符集是utf-8。并且建立数据库的时候也选择utf-8。
2.在程序中添加个过滤器。
官方过滤器:
Java code
importjava.io.IOException;importjavax.servlet.Filter;importjavax.servlet.FilterChain;importjavax.servlet.FilterConfig;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;/*** 过滤器,设置文字编码格式*/publicclassSetCharacterEncodingFilterimplementsFilter {protectedString encoding=null;protectedFilterConfig filterConfig=null;protectedbooleanignore=true;publicvoiddestroy() {this.encoding=null;this.filterConfig=null;
}/*** Select and set (if specified) the character encoding to be used to
* interpret request parameters for this request.
*
*@paramrequest The servlet request we are processing
*@paramresult The servlet response we are creating
*@paramchain The filter chain we are processing
*
*@exceptionIOException if an input/output error occurs
*@exceptionServletException if a servlet error occurs*/publicvoiddoFilter(ServletRequest request, ServletResponse response,
FilterChain chain)throwsIOException, ServletException {//Conditionally select and set the character encoding to be usedif(ignore||(request.getCharacterEncoding()==null)) {
String encoding=selectEncoding(request);if(encoding!=null)
request.setCharacterEncoding(encoding);
}//Pass control on to the next filterchain.doFilter(request, response);
}/*** Place this filter into service.
*
*@paramfilterConfig The filter configuration object*/publicvoidinit(FilterConfig filterConfig)throwsServletException {this.filterConfig=filterConfig;this.encoding=filterConfig.getInitParameter("encoding");
String value=filterConfig.getInitParameter("ignore");if(value==null)this.ignore=true;elseif(value.equalsIgnoreCase("true"))this.ignore=true;elseif(value.equalsIgnoreCase("yes"))this.ignore=true;elsethis.ignore=false;
}//------------------------------------------------------ Protected Methods/*** Select an appropriate character encoding to be used, based on the
* characteristics of the current request and/or filter initialization
* parameters. If no character encoding should be set, return
* null
.
*
* The default implementation unconditionally returns the value configured
* by the encoding initialization parameter for this
* filter.
*
*@paramrequest The servlet request we are processing*/protectedString selectEncoding(ServletRequest request) {return(this.encoding);
}
}XML code
setCharacterEncodingFilter包名.SetCharacterEncodingFilterencodingutf-8setCharacterEncodingFilter/*
作者: closewbq
发布时间: 2010-08-26
顶。。。。
作者: bb19891019
发布时间: 2010-08-26
大大,不是本地出问题,是程序上传到imhosted的服务器才出现的,本地的一切正常。
上传到空间后,中文查询不能用,插入中文字符到数据库中全部变成了问号。
数据库,页面,链接全都设置成了utf8。
我试一下过滤器。
作者: ghostgarlic
发布时间: 2010-08-26