使用MySQL的JDBC服务:UTF-8编码/解码 [英] JDBC Service with MySQL : UTF-8 encoding / decoding
问题描述
我尝试将Google数据库与Google云端硬盘中的文档同步。
我在Google App Script上写了一个小脚本来完成这项工作。
在处理字符编码时遇到了一些问题(默认字符集是UTF-8用collategeneral_ci)。我尝试了不同的方法(encodeURIComponent,小的UTF-8工具类,Blob,...),但没有任何工作。这里是一些小的示例代码:
var title = result.getString(title);
title = Utilities.newBlob(title,Utilities.Charset.UTF_8);
if(title.getDataAsString()!= file.getName()){
Logger.log('将文档%s从%s更新为%s',file.getId(),title .getDataAsString(),file.getName());
stmt_update_title.setObject(2,file.getId());
stmt_update_title.setObject(1,Utilities.newBlob(file.getName(),Utilities.Charset.UTF_8).getDataAsString());
stmt_update_title.addBatch();
}
当我从MySQL中检索时,我得到一个不正确的字符串?而不是重音字符('é','è','à')。当我更新时,字符串已损坏(但它在日志中打印得很好)。
感谢您的帮助。
jdbc:mysql:// host:port / instance?useUnicode = true& characterEncoding = UTF-8
I try to sync a MySQL DB with my documents in Google Drive. I wrote a small script on Google App Script to do this job.
I encounter some problem when dealing with character encoding (the default charset is UTF-8 with collate "general_ci"). I tried different ways (encodeURIComponent, small UTF-8 tool class, Blob, ...) but nothing works. Here is small sample code :
var title = result.getString("title");
title = Utilities.newBlob(title, Utilities.Charset.UTF_8);
if(title.getDataAsString() != file.getName()) {
Logger.log('Updating title for document %s from %s to %s', file.getId(), title.getDataAsString(), file.getName());
stmt_update_title.setObject(2, file.getId());
stmt_update_title.setObject(1, Utilities.newBlob(file.getName(), Utilities.Charset.UTF_8).getDataAsString());
stmt_update_title.addBatch();
}
When I retrieve from MySQL, I get an incorrect string with "?" instead of accented characters ('é', 'è', 'à'). When I update, the string is corrupted (but it prints well in the logs).
Thanks by advance for your help.
You need use the special connection string for the JDBC object like
jdbc:mysql://host:port/instance?useUnicode=true&characterEncoding=UTF-8
这篇关于使用MySQL的JDBC服务:UTF-8编码/解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!