从 SQLException.getMessage() 显示日语字符 [英] Displaying japanese characters from SQLException.getMessage()

查看:40
本文介绍了从 SQLException.getMessage() 显示日语字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的类,它显示数据库中包含日语字符的记录.类文件的字符编码为 UTF-8.我还更改了数据库以使用字符集 UTF-8.

I have a simple class which displays records having japanese characters from the database. The character encoding for class file is UTF-8. I have also altered database to use charset UTF-8.

在控制台上显示包含日文字符的记录没有问题.硬编码也没有问题.

There is no problem displaying the records having japanese characters on console. No problem with hard coded too.

但是当我尝试输入与旧记录之一具有相同用户 ID(表中的主键)的记录时.它抛出显示重复输入错误消息的 SQLException.但它显示垃圾字符.喜欢:

But when I try to enter the records with same userid (Primary key in table) as one of the old record. It throws SQLException that shows duplicate entry error message.But It shows junk characters. Like :

Duplicate entry 'ラケシュ12ï¼"ï¼"5' for key 1

那么到底是什么问题.为什么记录显示正确但不显示 SQLException.getMessage();

So what is the problem exactly. Why records are displayed correctly but not SQLException.getMessage();

推荐答案

ラケシュ12345

正确吗?

可能是在插入时发生了什么

Probably what happened when INSERTing was

  • 您对数据进行了正确的 utf8 编码,并且
  • SET NAMES latin1 -- 默认或错误地设置,以及
  • 存储文本的列(或表)是用 CHARACTER SET latin1 声明的,同样可能是默认设置.
  • You had correct utf8 encoding for the data, and
  • SET NAMES latin1 -- by default or by mistake, and
  • The column (or table) the text was stored into was declared with CHARACTER SET latin1, again possibly by default.

您可以通过执行以下操作来验证数据是否正确存储

You can verify that the data is stored correctly by doing

SELECT col, HEX(col) ...

如果您获取该字符串,则十六进制将是 E383A9E382B1E382B7E383A5EFBC91EFBC92EFBC93EFBC94EFBC95.请注意如何有 6 个十六进制组,从片假名的 E383 或全角数字"的 EFBC 开始.

If you fetch that string, the hex will be E383A9E382B1E382B7E383A5EFBC91EFBC92EFBC93EFBC94EFBC95. Notice how there are groups of 6 hex, starting E383 in the case of Katakana or EFBC for the "full width digits".

假设表仍然显示 latin1,没有数据丢失,并且 2-step ALTER 将修复它.总结:

Assuming the table still says latin1, no data is lost, and the 2-step ALTER will fix it. In summary:

ALTER TABLE Tbl MODIFY COLUMN col VARBINARY(...) ...;
ALTER TABLE Tbl MODIFY COLUMN col VARCHAR(...) ... CHARACTER SET utf8 ...;

其中长度足够大,另一个..."在列中已经有其他任何内容(非空等).

where the lengths are big enough and the other "..." have whatever else (NOT NULL, etc) was already on the column.

(直到最近,我才能对这个问题给出明确和完整的答案.)

(I could not give this clear and complete an answer to this question until very recently.)

这篇关于从 SQLException.getMessage() 显示日语字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆