0

I have coded a php project that is connected a mysql database. Also I have a jar file, which runs when a php url is called. 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. Until here everything is ok.

This is my php file that runs .jar file located in the server:

    <?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:

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

in the while loop:

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. I have also set db to "default character set utf8 default collate utf8_general_ci".

When I write a turkish character with php it saves to db, and I can see that. 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. But it doesn't work again.

Ali Yucel Akgul
  • 1,101
  • 8
  • 27
  • 53

1 Answers1

0

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. When you add getBytes("UTF-8") that's going to convert the internal string to UTF-8 and then return those bytes. You're then reinterpreting those bytes as 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.

chooban
  • 9,018
  • 2
  • 20
  • 36