Home > Database > Mysql Tutorial > java连接mysql数据库乱码怎么办

java连接mysql数据库乱码怎么办

WBOY
Release: 2016-06-07 16:16:58
Original
1237 people have browsed it

解决方法一: mysql安装时候的编码, 看下my.ini,有无 [mysql]default-character-set=utf8[client] default-character-set=utf8[mysqld] default-character-set=utf8 创建表的时候设置: DROP TABLE IF EXISTS `Company`; CREATE TABLE IF NOT EXISTS `Compa

 解决方法一:

mysql安装时候的编码,,

看下my.ini,有无 

[mysql] default-character-set=utf8 [client] default-character-set=utf8 [mysqld] default-character-set=utf8

创建表的时候设置:

DROP TABLE IF EXISTS `Company`; CREATE TABLE IF NOT EXISTS `Company` ( `Cname` VARCHAR(10) NOT NULL, `Caddr` VARCHAR(100), `Ctel` VARCHAR(16) )ENGINE=InnoDB DEFAULT CHARSET=UTF8;

jsp标头:

不行的话就尝试:

contentType="text/html;charset=gb2312"

解决方法二: 

连接mysql时(无论在从mysql读还是取数据的情况),指定使用的编码方式为utf-8,具体代码如下 

//装载mysql-jdbc驱动 Class.forName("com.mysql.jdbc.Driver").newInstance(); //连接数据库 Connection sqlCon = DriverManager.getConnection( "jdbc:mysql://localhost:3306/test?user=root&password=1&useUnicode=true&characterEncoding=utf-8" );

解决方法三: 

如果方法一不行那么在方法一的基础上对读入的字符串进行强制编码方式转换。 

String name = request.getParameter("name"); name= new String(name.getBytes("ISO-8859-1"),"utf-8");

或者:

String name =new String( rst.getString("name").getBytes("ISO-8859-1"),"utf-8"));

解决方法四: 

这个方法在有些文章里是首推的,我首先也是试了这个方法,但是好像不行。这里还是说一下,不知是不是我操作错误。还是先贴原文吧: 

"如果数据库中有中文的话而mysql默认的语言不是中文时要在配置文件C:winntmy.ini 中的 [mysqld] 里添加一行: 

default-character-set=utf-8 

然后重起mysql 

方法五:在tomca或者web项目下的web.xml里面更改:

UTFEncoding com.founder.chenph.Util.EncodingFilter_UTF8 encoding UTF-8 UTFEncoding *.jsp UTFEncoding *.do
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template