JDBC Service with MySQL : UTF-8 encoding / decodin

2019-05-19 23:40发布

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.

1条回答
Melony?
2楼-- · 2019-05-20 00:35

You need use the special connection string for the JDBC object like

jdbc:mysql://host:port/instance?useUnicode=true&characterEncoding=UTF-8
查看更多
登录 后发表回答