H2 Charset

6,081 views
Skip to first unread message

Abyric

unread,
Apr 9, 2009, 6:09:02 PM4/9/09
to H2 Database
Is there an option/possibility to change the default charset of H2 to
a charset I wish that can support special signs like chinese?
(UTF-8?)

Or is there a possibility to do this on table creation?

Thanks,
Jeff

Thomas Mueller

unread,
Apr 10, 2009, 1:22:44 PM4/10/09
to h2-da...@googlegroups.com
Hi,

> Is there an option/possibility to change the default charset of H2 to
> a charset I wish that can support special signs like chinese?
> (UTF-8?)

H2 already supports Unicode, so you don't need to change the character
set. Special signs like Chinese are already supported.

Regards,
Thomas

Abyric

unread,
Apr 23, 2009, 4:44:08 PM4/23/09
to H2 Database
Could you then please explain why my characters are turning all
gibberish when I import special characters?

An MySQL server transmits the text to the client (different location),
but after inserting it in H2 it looks gibberish. Is it that the
default font of my Ubuntu 8.04 perhaps doesnt support it?

Thanks

On Apr 10, 7:22 pm, Thomas Mueller <thomas.tom.muel...@gmail.com>
wrote:

Sharad

unread,
Apr 25, 2009, 5:23:10 AM4/25/09
to H2 Database

On Apr 24, 1:44 am, Abyric <killingd...@gmail.com> wrote:
> Could you then please explain why my characters are turning all
> gibberish when I import special characters?
>
I have very successfully used H2 database to store ,retrieve and
display Indian languages text in web applications.
If H2 can handle languages like Gujarati and Hindi properly using
UTF-8 encoding than there is no reason why any other languages like
( Chinese, Korean ) etc can not be stored or retrieved in H2.

The problem is that the JSP Engine ( Tomact / Resin
( www.caucho.com ) ) should be configured to handles UTF-8 encoded
text properly.
To make UTF-8 character set as default in your JSP page put following
line at the top:
<%@page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>

It is also sometimes necessary to put following meta-tags in HTML page
also
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Otherwise in Java, you will have to specify encoding every time you
construct a String from result set like this:
Suppose rslt is your JDBC ResultSet from which you are fetching a
table column text.
Than the code should be
String my_utf_string = new String( rstl.getString
("column_name").getBytes(), "UTF-8" )

In this case the row bytes are fetched from table column and from that
a new String is constructed using UTF-8 as the character encoding.

When you explicitly specify UTF-8 as default character set in JSP
page with directive @page pageEncoding="UTF-8", than the character
conversion is automatically handled for you by JSP engine.

Please also note that you must properly set character encoding in
HTML form elements like INPUT and TEXTAREA as well.

Hope this helps you
Thanks
Sharad Kelkar

Thomas Mueller

unread,
Apr 27, 2009, 1:10:25 AM4/27/09
to h2-da...@googlegroups.com
Hi,

As Sharad wrote, the problem is most likely somewhere outside H2.

> An MySQL server transmits the text to the client (different location),
> but after inserting it in H2 it looks gibberish.

I think the problems occurs when exporting from MySQL or when
importing to H2. How does this export / import work? Do you use a SQL
script file, or a CSV file? How do you export / import the data?

Regards,
Thomas

Sharad

unread,
Apr 27, 2009, 2:27:37 AM4/27/09
to H2 Database
Further Information on International Characters

If you are expecting MySQL to handle Indian Languages, Chinese,
Korean, or Japanese for that matter than care must be taken in writing
correct JDBC URL string as follows:

jdbc:mysql://localhost:3306/dbname?characterEncoding=UTF-8

Otherwise MySQL will predictably mess-up non-Latin Unicode text.

It is equally important for dump / restore operations to specify
character encoding through command line switches using MySQL command
line tools such as mysqldump .

But at least one good thing about MySQL command line tools is that it
works if we are careful in setting right switches.
Whereas my experience with third party GUI tools to work with MySQL
is totally disappointing for handling of international characters
other than English.

Thanks
Sharad Kelkar

Abyric

unread,
Apr 27, 2009, 8:41:29 PM4/27/09
to H2 Database
Thank you for the reactions.
I've tried Sharad's suggestion but it did not work.
Let me explain my situation more clear.

I currently have a tomcat server which talks with a MySQL server.
MySQL has utf8 encoding on it's tables. Also when i query the tables
everything is in a correct format.
The tomcat server fetches its data from MySQL. Doing a printline on
the fetched info tells me something goes wrong with the fetching. I've
tried adding ";characterEncoding=UTF-8" to the connection string
however
I get an exception that its not a valid encoding value.

Seemingly this isn't a H2 problem so i'll try to find my answer
elsewhere. Suggestions are welcome nonetheless.
Thank you both for the input !
Reply all
Reply to author
Forward
0 new messages