title: How to correctly write UTF-8 strings into MySQL through JDBC interface
body: <p>Connect to db:</p>
  <pre><code>public DBSource(ConnectionInfo ci) throws
          ClassNotFoundException, InstantiationException,
          IllegalAccessException, SQLException
  {
      Class.forName(&quot;com.mysql.jdbc.Driver&quot;).newInstance();
      String dbPath = String.format(
          &quot;jdbc:mysql://%s:%d/%s?user=%s&amp;password=%s&amp;characterEncoding=utf-8&amp;&quot; + 
          &quot;useUnicode=true&quot;, ci.host, ci.port, ci.dbName, ci.user, ci.password);
      conn = java.sql.DriverManager.getConnection(dbPath);
      prepareTables();
  }
  </code></pre>
  <p>Table creation code:</p>
  <pre><code>private void prepareTables() throws SQLException
  {
      java.sql.Statement stat = conn.createStatement();
  
      String query = &quot;set names utf8&quot;;
      stat.execute(query);
  
      query = &quot;set character set utf8&quot;;
      stat.execute(query);
  
      query = &quot;show variables like '%char%'&quot;;
      stat.execute(query);
      java.sql.ResultSet rs = stat.getResultSet();
      while (rs.next())
      {
          String k = rs.getString(1);
          String v = rs.getString(2);
          System.out.println(k + &quot; - &quot; + v);
      }
      
      query = &quot;drop table if exists clt&quot;;
      stat.execute(query);
      query = &quot;create table clt&quot;
              + &quot;(&quot;
              + &quot;  id bigint not null&quot;
              + &quot;, text varchar(50) not null&quot;
              + &quot;) default character set utf8&quot;;
      stat.execute(query);
  }
  </code></pre>
  <p>rows insertion:</p>
  <pre><code>public void visit(Insert i) throws SQLException
  {
      String query = &quot;insert into clt&quot;
              + &quot; (id, text) values (?, ?)&quot;;
      java.sql.PreparedStatement stmt = conn.prepareStatement(query);
      if (i.rowData.id == 12656697)
      {
          String toOut = &quot;&lt;&lt;&lt; &quot; + Long.toString(i.rowData.id) + &quot; - &quot; + i.rowData.text;
          System.out.println(toOut);
      }
      int it = 0;
      stmt.setLong(++it, i.rowData.id);
      stmt.setString(++it, i.rowData.text);
      stmt.execute();
      stmt.close();
  }
  </code></pre>
  <p>check data:</p>
  <pre><code> public void checkText() throws SQLException
  {
      java.sql.Statement stmt = conn.createStatement();
      String query = &quot;select id, text from clt where id = '12656697'&quot;;
      stmt.execute(query);
      java.sql.ResultSet rs = stmt.getResultSet();
      while (rs.next())
      {
          String k = rs.getString(1);
          String v = rs.getString(2);
          String toOut = &quot;&gt;&gt;&gt; &quot; + k + &quot; - &quot; + v;
          System.out.println(toOut);
      }
  }
  </code></pre>
  <p>output:</p>
  <pre><code>character_set_client - utf8
  character_set_connection - latin1
  character_set_database - latin1
  character_set_filesystem - binary
  character_set_results - utf8
  character_set_server - utf8
  character_set_system - utf8
  character_sets_dir - /usr/share/mysql/charsets/
  
  &lt;&lt;&lt; 12656697 - Апарати
  &gt;&gt;&gt; 12656697 - ???????
  </code></pre>
  <p><strong>Problem</strong>: In table I have &quot;<em>???????????</em>&quot; symbols at the text field.</p>
  <p><strong>expected string is</strong>: <em>Апарати</em><br />
  <strong>result</strong>: <em>???????</em></p>
  <p>It is some kind of <strong>magic</strong>?</p>
  <p>I resolved an issue... But still will appreciate if somebody can explain it to me.</p>
  <p>So.</p>
  <ol>
  <li>I added to my /etc/mysql/my.cnf lines suggested by Costis Aivalis<br />
  result the same</li>
  <li>I removed lines from my code:<br />
  query = &quot;set character set utf8&quot;;<br />
  stat.execute(query);</li>
  </ol>
  <p><strong>it is working!!! :)</strong></p>