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.
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.
An alternative to Sudhir's answer that keeps the same file structure is to set properties
to:
properties = {
databaseName = "test"
user = "user"
password = "password"
serverName = "localhost"
portNumber="3306"
characterEncoding="utf8"
useUnicode=true
}
utf8mp4 emoji issue
for spring-boot
Insert emoji does not work with spring-boot and MariaDB
spring.datasource.hikari.connection-init-sql=SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;
http://slick.lightbend.com/doc/3.2.0/config.html
Because slick supply HikariCP pool,I think hikari pool may solve this issue then try
mysql_orginal = {
connectionPool = "HikariCP"
driver = "com.mysql.jdbc.Driver"
connectionInitSql ="SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci"
url = "jdbc:mysql://127.0.0.1/table?useUnicode=true&characterEncoding=UTF-8"
keepAliveConnection = true
user = "root"
password = "123456"
numThreads = 4
poolName="mysql_orginal_pool"
}
val ORIGIN_DB = Database.forConfig("mysql_orginal")
libraryDependencies += "com.typesafe.slick" %% "slick-hikaricp" % "3.2.0"
it works
demo: https://github.com/cclient/ScalaMysqlSync
ref:
http://slick.lightbend.com/doc/3.2.0/database.html#databaseconfig
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.