title: Insert Chinese characters in MySql database using Java
body: <p>In my local environment it works fine, in my production environment, it doesn't.</p>
  
  <p>I've been reading a lot of answers on the internet. It seems I didn't get the answer I'm looking for.</p>
  
  <p>I can read Chinese characters from the database. When I write Chinese characters to the database they turn up like <code>'???'</code>. When I query the database to find a Chinese entry in the database, the result set is empty.</p>
  
  <p>I use <code>Java</code> for development, <code>jdk 1.6</code>. MySQL server: 5.0.91</p>
  
  <p>My web application has <code>utf-8</code> in the header of the web pages. In my servlets I set character encoding to <code>utf-8.</code> My tables and the columns in my tables use <code>utf8_general_ci</code>.</p>
  
  <p>I added <code>?useUnicode=yes&amp;characterEncoding=UTF-8</code> to the URL of my database. I added <code>SET NAMES 'UTF8' COLLATE 'utf8_general_ci'</code> before I do a query.</p>
  
  <pre><code>Character_set_connection = utf8
  </code></pre>
  
  <p>I noticed that on the production server, the <code>character_set_server = latin1.</code> However, when I changed <code>character_set_server</code> in my local environment to <code>latin1</code>, I could still insert Chinese characters.</p>
  
  <p>In my production environment, they tell me that others don't have any problem.</p>
  
  <p>I think I pretty much covered what has been suggested by many. Anyone any idea as to what else could be the problem?</p>
  
  <p>Update: quick temporary fix for those who need it, in the absence of a good solution.</p>
  
  <p>To insert the variable <code>name</code> in the database:</p>
  
  <p>Create a new column <code>name_as_bytes</code> using datatype <code>varbinary()</code></p>
  
  <p>Convert <code>name</code> to a bytes array<br>
  <code>byte[] nameAsBytes = name.getBytes("UTF-8");</code></p>
  
  <p>Insert <code>nameAsBytes</code> in <code>name_as_bytes</code> in the database</p>
  
  <p>Copy the <code>name_as_bytes</code> column to the <code>name</code> column, using convert() function</p>
  
  <pre><code>update myTable set name = CONVERT(nameAsBytes USING utf8) where ID = id;
  </code></pre>
  
  <p>With the extra query being performed, and the extra column added, this is not an ideal solution, but it helps to get your website going while looking for a solution.</p>