Esardes Esardes - 7 months ago 16
SQL Question

Scala/Slick JDBC encoding configuration with typesafe config

I am building a Scala application using Slick where I load my

MySQLDriverbackend
with
val db = Database.forConfig("mysql.dev")
and where my "application.conf" file looks like

mysql = {
dev = {
dataSourceClass = "com.mysql.jdbc.jdbc2.optional.MysqlDataSource"
properties = {
databaseName = "test"
user = "user"
password = "password"
serverName = "localhost"
portNumber="3306"
}
}
}


I am successfully saving data to my database which uses a default utf-8 encoding. However when trying to save the character 泗 I ended in my database with the dreaded
?
instead of input character.

I think I failed to declare that my connection needs to use UTF-8, however I couldn't find the option in the documentation of
forConfig
.

Do you know how I can fix that?
Thanks.

Answer

For MySQL, adding parameters useUnicode=true&characterEncoding=UTF-8 in your connection URL should solve the issue ie.

jdbc:mysql://<host>:<port>/<database>?useUnicode=true&characterEncoding=UTF-8

You could use this link to configure url.

Comments