我正在使用3.8.2版,插入新记录时,数据库中的utf8字符串有一些问题显示为问号(?),但它正确显示了旧记录(使用phpmyadmin插入)。
在这里我如何初始化MySQLPool
fun init(vertx: Vertx): MySQLPool {
val connectOptions = MySQLConnectOptions()
.setPort(Config.mysqlPort)
.setHost(Config.mysqlHost)
.setDatabase(Config.mysqlDatabase)
.setUser(Config.mysqlUsername)
.setPassword(Config.mysqlPassword)
.setCharset("utf8") // tried utf8mb4 also same result
.setCollation("utf8_general_ci") // also tried utf8mb4_general_ci
val poolOptions = PoolOptions().setMaxSize(Config.maxPoolSize)
return MySQLPool.pool(vertx, connectOptions, poolOptions)
}
mysql变量
character_set_client = utf8
character_set_connection = utf8
character_set_database = utf8
character_set_filesystem = binary
character_set_results = utf8
character_set_server = utf8mb4
character_set_system = utf8
表排序规则
utf8_general_ci
mysql创建表
CREATE TABLE `news` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Title` text NOT NULL,
`Image` text NOT NULL,
`Link` mediumtext NOT NULL,
`Summery` mediumtext NOT NULL,
`Story` mediumtext NOT NULL,
`Time` timestamp(6) NOT NULL DEFAULT current_timestamp(6) ON UPDATE current_timestamp(6),
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
谢谢
答案 0 :(得分:1)
似乎jvm中的默认字符集设置为 windows-1252 ,将其更改为 UTF-8 已解决
System.setProperty("file.encoding", "UTF-8")
val charset = Charset::class.java.getDeclaredField("defaultCharset")
charset.isAccessible = true
charset.set(null,null)
setCharset(“ utf8”)和 setCollation(“ utf8_general_ci”)选项是在连接级别配置的,但对于编码的字符集并没有真正的影响,这就是为什么将编码值编码为默认的jvm字符集的原因
答案 1 :(得分:0)
我认为这是您的问题Arabic language in php/mysql appears "????" question marks in html
的答案您必须在首次连接中设置字符集。