0

I have a website (Spring + Hibernate + MS SQL Server) that accepts user input in traditional Chinese and saves it in the database. For example, a user may enter the following:

31~40歲

I notice that it is saved as the following in the database

31~40歲

Note that the saved text can be correctly retrieved and displayed in the browser. I also use Apache POI package to export user input into Excel. However, in Excel, the text is

31~40歲

Not

31~40歲

In my export code, I simply used:

cell.setCellValue(_get_data_from_database_);

What is missing in my work? In the database layer? Or in export to Excel?

halfer
  • 19,824
  • 17
  • 99
  • 186
curious1
  • 14,155
  • 37
  • 130
  • 231
  • 2
    First I would check **why** the Unicode char ~ FULLWIDTH TILDE is stored as HTML entity `~` while the Chinese character is stored as Unicode. Because if ~ would also be stored as Unicode, then the whole problem would disappear. Otherwise see http://stackoverflow.com/questions/14998726/replace-html-codes-with-equivalent-characters-in-java. – Axel Richter Mar 14 '17 at 18:18
  • Axel, you right. The html entity is produced by another tool for HTML sanity. Thanks so much for pointing me to the right direction!!! – curious1 Mar 15 '17 at 06:05
  • 1
    You're welcome! This is an frequently made error. The escaping of characters having special meaning cannot be general but must be context dependent. While storing something into a DB, the context is **not** HTML but SQL. So the escaping must be in context SQL (escape characters having special meaning in SQL) and not in context HTML. But while inserting DB content into HTML, the context is HTML. So **there** the escaping must be in context HTML. – Axel Richter Mar 15 '17 at 07:21
  • Axel, you may want to summarize your comments in a post. I will select it as the answer. Cheers! – curious1 Mar 15 '17 at 13:40

0 Answers0