JAVA查询MYSQL时乱码异常

JAVA查询MYSQL时乱码错误

我用JAVA向MYSQL查询数据的时候,后台打印出的中文都没问题,可是编写SQL语句进行数据库查询时就提示乱码,我的语句是:String sql=”set names ‘gbk’;select * from commitment where majorclass like ‘%”+department+”%’ and tname='”+tname+”‘ and title='”+title+”‘ ;”;其中的department和tname以及title在后台打印都正常显示,可是执行的时候就提示乱码了。我的MYSQL编码是前台用GBK编码,后台存储用UTF8,my.ini文件是这样的

[client]

port=3306

[mysql]

default-character-set=gbk  

# SERVER SECTION

# ———————————-

#

# The following options will be read by the MySQL Server. Make sure that

# you have installed the server correctly (see above) so it reads this 

# file.

#

[mysqld]

# The TCP/IP Port the MySQL Server will listen on

port=3306

#Path to installation directory. All paths are usually resolved relative to this.

basedir=”D:/MyFile/MYSQL/”

#Path to the database root

datadir=”C:/ProgramData/MySQL/MySQL Server 5.1/Data/”

# The default character set that will be used when a new schema or table is

# created and no character set is defined

default-character-set=utf8

这个该怎么解决呢



1 SET NAMES utf-8;

2 将你的SQL 字串字段转为 utf-8 编码…




页面编码和数据库编码保持一致就可以了。

JAVA查询MYSQL时乱码异常

相关文章:

你感兴趣的文章:

标签云: