Jdbc (mysql) unicode behavior

clojure, jdbc, mysql, utf-8

Solution

If you used the example from the Wiki, one obvious thing missing from there is setting the connection encoding. If you didn't catch that here is the untested example with encoding:

(use 'clojure.java.jdbc)

(let [db-host "localhost"
      db-port 3306
      db-name "a_database"]

  (def db {:classname "com.mysql.jdbc.Driver" ; must be in classpath
           :subprotocol "mysql"
           :subname (str "//" db-host ":" db-port "/" db-name "?characterEncoding=UTF-8")
           ; Any additional keys are passed to the driver
           ; as driver-specific properties.
           :user "a_user"
           :password "secret"}))

Problem

I have a Mysql database with two UTF-8 tables. Let's call them Source and Destination. Using clojure.java.jdbc I successfully retrieve a field from Source, a field which contains the 'ă' symbol. Upon trying to save that string in the Destination, I find a '?' in the place where the 'ă' symbol should have been. This is the sequence that actually saves the record: ``` (jdbc/update-or-insert-values "destination" ["lexeme = ?" word] record) ``` Does anybody have an idea as to why this is happening?

Original source