Java 中文问题一直困扰许多学习者。总结了下面的一些情况的解决方法。
希望对大家有帮助。
连接 Mysql Database Server:
-------------------------------------------------------------------------------
mysql 不支持 unicode,所以比较麻烦。
将 connectionString 设置成 encoding 为 gb2312
String connectionString
= "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gb2312";测试代码:
String str = "汉字";
PreparedStatement pStmt = conn.prepareStatement("INSERT INTO test VALUES (?)");
pStmt.setString(1,str);
pStmt.executeUpdate();数据库表格:
create table test (
name char(10)
)
连接 Oracle Database Server
-------------------------------------------------------------------------------
在把汉字字符串插入数据库前做如下转换操作:
String(str.getBytes("ISO8859_1"),"gb2312")测试代码:
String str = "汉字";
PreparedStatement pStmt = conn.prepareStatement("INSERT INTO test VALUES (?)");
pStmt.setString(1,new String(str.getBytes("ISO8859_1"),"gb2312");
pStmt.executeUpdate();
Servlet
-------------------------------------------------------------------------------
在 Servlet 开头加上两句话:
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");JSP
-------------------------------------------------------------------------------
在 JSP 开头加上:
<%@ page contentType="text/html; charset=gb2312" %>
希望对大家有帮助。
连接 Mysql Database Server:
-------------------------------------------------------------------------------
mysql 不支持 unicode,所以比较麻烦。
将 connectionString 设置成 encoding 为 gb2312
String connectionString
= "jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=gb2312";测试代码:
String str = "汉字";
PreparedStatement pStmt = conn.prepareStatement("INSERT INTO test VALUES (?)");
pStmt.setString(1,str);
pStmt.executeUpdate();数据库表格:
create table test (
name char(10)
)
连接 Oracle Database Server
-------------------------------------------------------------------------------
在把汉字字符串插入数据库前做如下转换操作:
String(str.getBytes("ISO8859_1"),"gb2312")测试代码:
String str = "汉字";
PreparedStatement pStmt = conn.prepareStatement("INSERT INTO test VALUES (?)");
pStmt.setString(1,new String(str.getBytes("ISO8859_1"),"gb2312");
pStmt.executeUpdate();
Servlet
-------------------------------------------------------------------------------
在 Servlet 开头加上两句话:
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");JSP
-------------------------------------------------------------------------------
在 JSP 开头加上:
<%@ page contentType="text/html; charset=gb2312" %>
中国:\u4e2d\u56fd (十六进制) 中国(十进制)
只要在 JSP 开头加上:
<%@ page contentType="text/html; charset=gb2312" %>
就显示正常了
1.把连接mysql的url定义为(主机名和数据库名改一下):
jdbc:mysql://192.168.1.2:3306/worklog?useUnicode=true&characterEncoding=GBK2.在工程里添加一个过滤器:package com.multim.technic.util;import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;/**
* <p>Title: 工作日志管理系统</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author wingtrace
* @version 1.0
*/public class SetCharacterEncodingFilter
extends HttpServlet
implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true; public void destroy() {
this.encoding = null;
this.filterConfig = null;
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException { // Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null) {
request.setCharacterEncoding(encoding);
}
} // Pass control on to the next filter
chain.doFilter(request, response); } public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null) {
this.ignore = true;
}
else if (value.equalsIgnoreCase("true")) {
this.ignore = true;
}
else if (value.equalsIgnoreCase("yes")) {
this.ignore = true;
}
else {
this.ignore = false;
} } protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}}3.在web.xml文件里面加入: <filter>
<filter-name>setcharacterencodingfilter</filter-name>
<filter-class>com.multim.technic.util.SetCharacterEncodingFilter</filter-
class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>以上解决方案在 tomcat5.0.25+mysql4 中通过。