解决插入数据库中文乱码问题
              
其实中文问题挺好解决的,关键的就两个地方设置对了就ok了。
第一、
1、struts+jdbc情况下:
EncodeActionServlet继承ActionServlet
package classmate;

import org.apache.struts.action.*;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;

public class EncodeActionServlet extends ActionServlet
{
    protected void process(
        HttpServletRequest request,
        HttpServletResponse response)
        throws IOException, ServletException
    {
        HttpSession session = request.getSession(true);
        if (session.isNew())//session超时
        {
            response.sendRedirect(
"/logout.jsp");
            return;
        }
        request.setCharacterEncoding(
"latin1"); //进行统一的中文转码latin1  <1>
        super.process(request, response);
    }
}
EncodeActionServlet在web.xml完成部署

  <!-- Action Servlet Configuration -->
  <servlet>
    <servlet-name>actionServlet</servlet-name>
    <servlet-class>classmate.EncodeActionServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Action Servlet Mapping -->
  <servlet-mapping>
    <servlet-name>actionServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
还有一个关键的部分是在struts-config.xml中的数据库连接部分如下:
<data-sources>
<data-source key=
"oracleDB1" type="org.apache.commons.dbcp.BasicDataSource">
<set-property property=
"driverClassName"
value=
"org.gjt.mm.mysql.Driver" />
<set-property property=
"url"
value=
"jdbc:mysql://localhost/struts?
                useUnicode=true&amp;characterEncoding=latin1
" />  <2>
<set-property property=
"maxActive"
value=
"5"/>
<set-property property=
"username"
value=
"root"/>
<set-property property=
"password"
value=
"123456"/>
<set-property property=
"autoCommit"
value=
"true"/>
</data-source>
</data-sources>
2、如果是struts+hiberante就在hibernate.cfg.xml的配置的数据库连接部分如下:
<?xml version=
"1.0" encoding="GB2312" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration
DTD
//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name=
"hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<!-- ?useUnicode=true&amp;characterEncoding=UTF-8 -->
    <property name=
"hibernate.connection.url">jdbc:mysql://localhost/struts?
              useUnicode=true&amp;characterEncoding=latin1</property>  <3>
    <property name=
"hibernate.connection.username">root</property>
    <property name=
"hibernate.connection.password">123456</property>
    
    <property name=
"show_sql">true</property>
    <property name=
"dialect">net.sf.hibernate.dialect.MySQLDialect</property>
    <mapping resource=
"hb/User.hbm.xml" />
  </session-factory>
</hibernate-configuration>
第二、还是在struts+jdbc下用自己的RequestProcessor去控制。
MyRequestProcessor.java
package StudyNews;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.RequestProcessor;

import org.apache.struts.config.ForwardConfig;
import org.apache.commons.logging.Log;


public class MyRequestProcessor extends RequestProcessor {

public MyRequestProcessor() {}

protected boolean processPreprocess( HttpServletRequest request,
                                     HttpServletResponse response ){

try{
   request.setCharacterEncoding(
"latin1");//此处为自己用的数据库的字符集 <4>

}
catch(Exception ex){
System.out.println(
"字符集设置失败");

}

return true;
}

}
它在struts-config.xml中配置如下:
<controller processorClass=
"StudyNews.MyRequestProcessor" />
数据库连接方面和上面一样。

总结:关键是<1><2><3><4>处的字符集设置要与自己数据库用的字符集一致。以前在struts
框架下往数据库插入中文失败的原因在于只设置了<1>处而没设置<2>处。









      
lunzi   2006-05-26 00:49:06 评论:2   阅读:8354   引用:0
latin1是我装的时候随便选的一个 @2006-05-26 20:18:06  lunzi
我对于mysql数据库基本的操作已经很熟练了,不过其中字符集用那一种及它门之间有什么区别,还不是太清楚,谢谢hofman指导,马上改之.
latin1 @2006-05-26 17:34:42  hofman
encoding用latin1实在怪里怪气的,用GBK/GB18030/utf-8呀。action里面用sendRedirect也不好,用mapping.findForward("login")比较规范。

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog