java learnging

          一塊探討JAVA的奧妙吧
          posts - 34, comments - 27, trackbacks - 0, articles - 22

          MySql中的中文亂碼解決方案

          Posted on 2005-01-13 14:45 bigseal 閱讀(1098) 評論(0)  編輯  收藏

          上次測試使用hibernate,遇到了中文的亂碼問題,今天在web中使用hibernate又遇到了亂碼的問題。但是上次解決后沒有把解決方案記錄下來,今天重新上網搜索了一把。并將解決方案記錄在下面便于以后再遇到時查看用。

          Hibernate中配置Mysql數據庫如下:

          hibernate.dialect net.sf.hibernate.dialect.MySQLDialect

          hibernate.connection.driver_class org.gjt.mm.mysql.Driver

          #hibernate.connection.driver_class com.mysql.jdbc.Driver

          hibernate.connection.url jdbc:mysql://localhost:3306/test

          hibernate.connection.username root

          hibernate.connection.password password

          hibernate.commection.url的值后面加上字符串“?useUnicode=true&characterEncoding=GBK”就可以解決了。

          即修改后的url為:

          hibernate.connection.url jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK

          Hibernate還有一種配置文件是xml格式的,文件名為:hibernate.cfg.xml

          xml文件中配置MySql數據庫的定義如下:

              <session-factory name="/jndi/ContactsSessionFactory">

                 <!-- properties -->

                 <property name="hibernate.connection.driver_class">

          org.gjt.mm.mysql.Driver

          </property>

                 <property name="hibernate.connection.url">

          jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=GBK

          </property>

                 <property name="hibernate.connection.username">root</property>

                 <property name="hibernate.connection.password">password</property>

                 <property name="hibernate.dialect">

          net.sf.hibernate.dialect.MySQLDialect

          </property>

                 <property name="hibernate.connection.pool_size">4</property>

                 <property name="hibernate.show_sql">true</property>

                 <!-- mapping files -->

                 <mapping resource="hello/Message.hbm.xml"/>

              </session-factory>

          注意由于在XML文件中&符號是轉義符,因此需要對其進行轉義。即使用&amp;來代替。

           

                  

           

          JSP連接MYSQL數據庫如果有中文存取的情況會出現亂碼,網頁上中文顯示一堆“?“,
          需要做如下改變
          1. 連接串:
           String mysqlDriver = "org.gjt.mm.mysql.Driver";                        //數據庫驅動,又作"com.mysql.jdbc.Driver"
            String mysqlUrl = "jdbc:mysql://localhost:3306/bbs?useUnicode=true&characterEncoding=gb2312";    //數據庫連接字串
            String mysqlUser = "root";                                             //數據庫用戶名
            String mysqlPsw = "";                                                  //數據庫密碼
           
            Class.forName("org.gjt.mm.mysql.Driver").newInstance();   //注冊驅動
            conn = DriverManager.getConnection(mysqlUrl,mysqlUser,mysqlPsw);  //得到連接

          2.請求對象設置:
          <% request.setCharacterEncoding("gb2312");%>
          將jsp的請求對象的字符集設置為支持中文

           

           

          這中方法還沒用過,先收藏起來,有空的時候研究一下。


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 施秉县| 达尔| 通榆县| 思茅市| 龙山县| 石台县| 皮山县| 邵武市| 平原县| 淳化县| 正宁县| 合水县| 屯门区| 科尔| 台东县| 灯塔市| 东光县| 手机| 冕宁县| 红河县| 双桥区| 周口市| 彭泽县| 民和| 嘉荫县| 蒲城县| 万安县| 彰化市| 东山县| 九龙坡区| 庐江县| 清镇市| 高陵县| 宿松县| 靖西县| 乃东县| 武威市| 古交市| 怀安县| 澳门| 绍兴县|