×

Loading...

被一个问题困惑了几天,望有人帮助.一Java Web App, 和第一个MySql DB Schema连接,UTF8没有任何问题,再和另一个schema相连,从DB读UTF8没有问题,但往里写UTF8就是乱码.

DB default cha set, request, response, jdbc connection string, table language option, etc, etc, 都查过了....
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术讨论 / 被一个问题困惑了几天,望有人帮助.一Java Web App, 和第一个MySql DB Schema连接,UTF8没有任何问题,再和另一个schema相连,从DB读UTF8没有问题,但往里写UTF8就是乱码.
    DB default cha set, request, response, jdbc connection string, table language option, etc, etc, 都查过了....
    • 问一个比较弱的问题(因为没有用过MySQL),以UTF8的格式写数据库?数据库内部以UTF8保存?
      • 好像是这样的。
        • 数据库内部用UCS-2保存应该会更好点吧。
          • 你讲的是UTF-16?MySQL 好像到现在为止并不支持16位元。
            • 噢!谢谢!
    • 我曾经在PHP中也遇到过这个情况。
      • 你后来怎么解决的? 都改用一个SCHEMA?
        • 这是我的方法。不知对你有没用。
          Unicode Like Chinese Japanese Korean Characters In and Out MySQL Through PHP

          Both MySQL and PHP support the Unicode, namely UTF-8. To be able to store into and retrieve these Unicode characters in MySQL database, one would need to do following things:

          MySQL needs to be informed through PHP. So, following code need to be placed just after the connection. If the connection is centralized, this code is better to be in that centralized file:

          mysql_query("SET NAMES 'UTF8'");

          For all your PHP files generates HTML codes, please include following code at top of the file:

          header("Content-Type: text/html; charset=UTF-8");

          For all your HTML files, following code is needed in the head of HTML:

          ‹ meta http-equiv="Content-Type" content="text/html; charset=UTF-8" ›

          Done. Please note, in first piece of PHP code, it is UTF8 and in second PHP code and last HTML code it is UTF-8. Else, it won’t work properly.
          • The way of handling UTF8 is similar in Java Web code. It is an Ajax http post request to send form info to server. When posted data is read back from response, it displays OK on web page. But it becomes crappy when saved into table.
            But if you put UTF8 text in table, and let system read it and display on web, no problem at all.

            The above is for the second web application. for another application on another schema, both read / write work fine.

            Two applications are sharing basically the same codeset.

            In Mysql, table's charset is utf8, collation is utf8_general_ci. default chaeset for database is utf8.
            • I had same problem as what you have, unable to write into database properly.
              That was because I used some other codes for which I could remember now. In some classes it worked while in other classes it did not work. I was never being able to figure out what was wrong.

              After I changed to mysql_query("SET NAMES 'UTF8'"), all problems disappeared.
              • Actually in your MySQL, it looks like a crappy, but indeed it is not.
                I remember somehow I managed to show it correctly in HTML by setting to different charset, while the correct chars in MySQL displayed crappy in HTML. This test implies the problem was with the default writing setting in MySQL, or the communication between MySQL and PHP/Java. This thought lead me eventually to the solution of this:

                mysql_query("SET NAMES 'UTF8'");

                Once for all. Done. You may wish to try too. Clean up all your rest language settings except you need to tell MySQL these words: "SET NAMES 'UTF8'".
                • I actually have two lines..:-). I can see Chinese right in the table, right after the text is written down through the first application. But for the second app, the text is crappy.
                  stmt.execute("SET NAMES 'utf8'");
                  stmt.execute("SET CHARACTER SET 'utf8'");



                  The only difference between two apps is: I use normal form post in the first app, and Ajax post in the second app. I traces those texts all the way posted to server, to DB, the problem is probably in jdbc connection, but I have similar connection string between two apps....

                  jdbc:mysql://xxx.xxx.xxx.xxx:xxxx/db_name?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8

                  11.
                  • If I were you , I would try to remove stmt.execute("SET CHARACTER SET 'utf8'") to see if it does work.
                    • 你还别说:你对了!!!有饭局的时候,一定要叫上我.
                      • 你没蒙我?我太高兴了。我当时就是这么蒙出来的。没法弄明白里面的道理。
                        牛哥一直是我的崇拜对象。
                        • 我也不知道为什么. 哈哈.
                          • UTF8 和 XML-UTF8是有区别的,是不是因为这原因?
                            • 不明白你说的XML-UTF8..? 写DATA时用的是standard JDBC API, setString(), 和XML没关系.
                              • 我胡乱猜测呢,刚才google了一下,感觉和MySql的unicode mapping table有关系,另外呢有人提到了Clinent-Server shakehands,感觉好像是Database server通过检测client的locale Code page从而确定采用何种unicode mapping有关系(夏猜测的)
                                • Interesting! Can you put more detail on "locale Code page"? At app level, for example, request/response model, how to check it?
                                  • 牛歌, 根据你问这个问题, 说明你还不能实现: 根据CLIENT的BROWSER LANGUAGE, 自动显示相对应的多国语言. 就象GOOGLE, YAHOO...它们能自动显示.
                                    • I leave it to user to decide, if the app is a portal. When user first contact app, he will choose his favored lang option, color scheme option, date/time format, various hide-when, and those options will saved in his profile and
                                      will be immediately available in next visit after login.

                                      I just make sure what I deliver is a UTF-8 page.
                                      • PM me your phone number, I hope I can help you, Sir.
                                        • 大侠,帮我看一下我的方法管不管用:
                                          我在Index.html里定义:<script language='Javascript' src='/php/contents.js.php' type='text/javascript'></script>

                                          contents.js.php的内容如下:

                                          <?php
                                          require_once("language.php");
                                          $lang = detectLanguage(); // Get the request.
                                          if ("en"==$lang)
                                          { // Print back as plain javascript.
                                          print
                                          <<<BRACKET
                                          ; var button_cancel = 'Cancel'
                                          ; var button_close = 'Close'
                                          ; var button_save = 'Save'
                                          。。。
                                          BRACKET;

                                          } else if ("zh"==$lang)
                                          { // Print back as plain javascript.
                                          print
                                          <<<BRACKET
                                          ; var button_cancel = '取消'
                                          ; var button_close = '关闭'
                                          ; var button_save = '保存'
                                          。。。
                                          BRACKET;
                                          };
                                          ?>

                                          请问有什么问题吗?我没试过,因为我没有PHP背台。我怀疑我的方法比Google们的简单,因为语言部分我放在PHP文件里而不是放在Database里。
    • 难道是BOM的问题?另一个不是好好的吗。
    • 在什么地方看到的乱码?
    • 先瞎猜一个。。另外,牛哥用什么TRACER? 应该有些详情数据出来。。。
    • 钓鱼贴??