简体   繁体   English

PHP从MySQL UTF-8编码到Java

[英]php to java from mysql utf-8 encoding

I have coded a php project that is connected a mysql database. 我已经编码了一个连接mysql数据库的php项目。 Also I have a jar file, which runs when a php url is called. 我也有一个jar文件,该文件在调用php url时运行。 Through php I take parameters from user and save them to database and after that my jar file gets connected to database and reads the specified rows. 通过php,我从用户那里获取参数并将其保存到数据库中,然后我的jar文件连接到数据库并读取指定的行。 Until here everything is ok. 直到这里一切都还好。

This is my php file that runs .jar file located in the server: 这是我的php文件,运行位于服务器中的.jar文件:

    <?php
mysql_query("SET NAMES utf8");
header("Content-Type: text/html;charset=UTF-8");


echo shell_exec('java -jar /home/path/path/path/my.jar');

?>

and my jar file as follows: 和我的jar文件如下:

Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/somedb?useUnicode=true&characterEncoding=UTF-8", "root", "pass");

in the while loop: 在while循环中:

while (resultSet2.next()) {


        title2 = new String(resultSet2.getString("title").getBytes("UTF-8"), "UTF-8");
        url2 = new String(resultSet2.getString("url").getBytes("UTF-8"), "UTF-8");
        id = new String(resultSet2.getString("id").getBytes("UTF-8"), "UTF-8");
        date = new String(resultSet2.getString("date").getBytes("UTF-8"), "UTF-8");

        list.add(title2);
        list2.add(url2);
        list4.add(id);
        list5.add(date);


    }

when I retrieve something from db, everthing works perfectly. 当我从db中检索某些内容时,一切都可以正常运行。 I have also set db to "default character set utf8 default collate utf8_general_ci". 我还将db设置为“默认字符集utf8默认归类utf8_general_ci”。

When I write a turkish character with php it saves to db, and I can see that. 当我用php编写土耳其语字符时,它保存到db中,我可以看到。 But when I try to retrieve those turkish characters with my jar it only shows question marks. 但是,当我尝试用罐子检索那些土耳其字符时,它仅显示问号。 I tried almost everything but no chance. 我尝试了几乎所有内容,但没有机会。

Any help is greatly appriciated. 任何帮助都非常有用。

edit: I also changed UTF-8 to ISO-8859-1 in the jar file. 编辑:我也将jar文件中的UTF-8更改为ISO-8859-1。 But it doesn't work again. 但这不再起作用。

If all the connection settings are correct then 如果所有连接设置均正确,则

title2 = resultSet2.getString("title")

...should be sufficient. ...就足够了。 This will retrieve the information from the database and interpret the string using the connection settings, and then convert it to UTF-16 which is Java's internal representation. 这将从数据库中检索信息,并使用连接设置解释字符串,然后将其转换为Java内部表示的UTF-16。 When you add getBytes("UTF-8") that's going to convert the internal string to UTF-8 and then return those bytes. 当您添加getBytes(“ UTF-8”)时,它将内部字符串转换为UTF-8,然后返回这些字节。 You're then reinterpreting those bytes as UTF-8. 然后,您将这些字节重新解释为UTF-8。

I know that MySQL ignores connection options from the config file when connecting as root, so try creating a new user and running it again. 我知道MySQL以root身份连接时会忽略配置文件中的连接选项,因此请尝试创建一个新用户并再次运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM