日期:2014-05-16  浏览次数:20916 次

mysql 不能插入中文字符 中文字符读写乱码问题
环境 : mysql-5.1.43-win32.msi + myeclipse8.0 + apache-tomcat-6.0.24.exe + jdk-6u18-windows-i586se版.exe

关键代码

mysql配置文件 my.ini 设置其字符集为gbk (重启有效)

[code="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:/Program Files/MySQL/MySQL Server 5.1/"

#Path to the database root
datadir="D:/Program Files/MySQL/MySQL Server 5.1/Application Data/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=gbk



# The default storage engine that will be used when create new tables when
default-storage-engine=INNODB
Servlet中从页面读取字符串的处理

[code="java"]public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
try{
username = new String(username.getBytes("ISO-8859-1"));


UserDao userDao = new UserDaoImpl(new DBMGJbdc());
ArrayList userList = userDao.getUserListByUsername(username);
request.setAttribute("userList", userList);
ServletContext servletContext = this.getServletContext();
servletContext.getRequestDispatcher("/queryResult.jsp").forward(request, response);
}catch(Exception ex){
ex.printStackTrace();
}

}
JSP页面中设置页面字符集为GBK

[code="html"]

数据库使用Mysql Workbench设计,字符集均使用默认字符集